Nullable USTRUCT in Blueprints?

In my game I store tasks for characters in USTRUCT models (they have to be USTRUCTs to inherit from FTableRowBase).

If I need variable task to be nullable in C++ code (e.g. to store a currently active task) I can store it as FMyTask* or TSharedPtr. If it’s equal to nullptr or TSharedPtr is not valid, that means that there is no current task at the moment.

But blueprints don’t allow you to have TSharedPtr or raw pointers of USTRUCTs.

So far the best idea I had is to wrap it inside a UObject, which can be nullable, but that seems like an overkill.

Do you know if there a better solution?

Blueprints don’t support pointers and UObjects can be stored in pointers only so those are only pointers supported, it’s limitation of VM. It’s support refrences, but they can’t be null. Also TSharedPtr made to be used outside of UE4 reflection system where GC can’t direcly monitor varables so Blueprint don’t support those either because i most cases it does not need to, because you need to use UPROPERTY() either way.

So you need to work around it, make something inside you struct that informce that there is no task currently worked on. And as you already noticed, you are free to use pointers in C++ whatever you like :slight_smile:

So, it’s either a flag stored inside a USTRUCT

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = TaskParameter) 
bool bIsValid = true;

or a UObject pointer wrapper like UMyTaskPtr?

That will work, whatever you find fit for your users. I make it more readable and name it bIsActive or something like that insted bIsValid