Download

FText variable becomes garbage collected

I have class derived from uobject
class GWGENERALS_API UGWArmor : public UObject
and variable

public:

UPROPERTY(BlueprintReadOnly)
FText ArmorType;

When the game starts, the variable is initialized and there is a value in it, I checked it with a debugger. But after about 10-20 seconds, the variable becomes nullptr and any access to it, for example ArmorType.ToString() causes an error. This behavior appears only in the packaged game. When game runs in the editor`s viewport, everything works fine and the text has value at every time

Are you initializing the value in the constructor or BeginPlay()?

Solved. Ive marked class property FText ArmorType as UPROPERTY(BlueprintReadOnly) but didnt marked class owner UObject Armor as UPROPERTY. So not only FText was garbage collected, but whole owner uobject.

Hi Den,
How do you set a class owner into a UProperty ?

He means the UGWArmor wasn’t being held on by anything.




// In my player some where...

UPROPERTY()
UGWArmor* MyArmor; // This UPROPERTY holds a reference to the armor, so GC won't touch it.



You can also add its to root like Some property like Static mesh, or text variable => gameproperty->AddtoRoot();

This has saved me from garbage collection as well. :slight_smile:

@ExtraLifeMatt is that true ?

Adding to root is a bad way to do it, unless you absolutely need that object to NEVER be garbage collected. Generally you want some sense of ownership (e.g., My Player owns their armor, so if my player ever is destroyed - it’s safe to destroy the armor as well).

The description you gave is allowed only for components ? Like they have


activated and deactivate

functions which can help the GC to know if that has a owner or not.

That far I know, could you tell me how you do the same for different objects with the ownership concept ?