Announcement

Collapse
No announcement yet.

Problems with Garbage Collector and Graph pins

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problems with Garbage Collector and Graph pins

    Hello!

    I am reviewing an old code that used to work in older versions of the editor but it has stopped to work now (4.23)
    The code in question was extracted from the book "Unreal Engine 4 Scripting with C++ Cookbook"

    What I am trying to do in this code is to create an editor for the default value of a pin. The type of the pin is a custom asset.

    So I have created a pin factory (FMyCustomAssetPinFactory) that returns a custom pin, SGraphPinCustomAsset.

    In the custom asset I overrided the function GetDefaultValueWidget

    Code:
    if ( GraphPinObj->LinkedTo.Num() > 0 )
      {
        return SGraphPin::GetDefaultValueWidget();
      }
      else
      {
        UMyCustomAsset* currentAsset = Cast<UMyCustomAsset>( GraphPinObj->DefaultObject );
        FColor currentColor;
        if (currentAsset)
        {
          currentColor = FColor::FromHex(currentAsset->hexColor);
        }
        else
        {
          currentColor = FColor::Orange;
          UMyCustomAsset* newAsset = NewObject<UMyCustomAsset>();
          newAsset->hexColor = currentColor.ToHex();
          GraphPinObj->GetSchema()->TrySetDefaultObject(*GraphPinObj, newAsset);
        }
        return SNew( SColorPicker )
          .UseAlpha( false )
          .TargetColorAttribute( currentColor )
          .OnColorCommitted_Lambda( [ this ]( FLinearColor _selectedColor )
        {
          // UPDATE THE ASSETS VALUE
        } );
      }
    Now, I create a function in a blueprint that accepts a UMyCustomAsset as input and the pin is shown as it should, with the custom default value picker. I can change its value and everything.

    The problem is that every time I hit the Compile button the value resets to the default value (FColor::Orange).

    Adding some breakpoints and tracing back I discovered that when I compile the asset there is a garbage collection that is destroying the value associated to the DefaultObject variable of the pin (the UMyCustomAsset).

    Why is that?
    I thought that once I passed the ownership of the asset to the pin it was able to protect it from the garbage collection. Is there something else I have to do?

    Thank you





Working...
X