Download

Managing Pointers

Is there some general idea how to manage pointers that are passed between C++ code and blueprints?

For example lets take a spawn actor method. We spawn some UObject that has its blueprint. SpawnActor function returns a pointer to that Actor and then we return this pointer for use in blueprints. How do you manage it further? If I didn’t use method returning pointer to blueprint I could use smart pointers or some unreal engine pointers but blueprints doesn’t support it. What now? How to manage pointers like these?

Hey,

Assuming that you spawn an actor in a C++ function of a BP object (that is parented to C++ object like all BPs are), and want to use its pointer in blueprints, all you have to do is make sure that the variable storing the reference is C++ is declared as:


UPROPERTY(BlueprintReadOnly) //or BlueprintReadWrite
AActor* YourPointerHere;

In your cpp:



YourPointerHere = GetWorld()->SpawnActor...


Then it’s a simple matter of right clicking in your blueprint graph, writing “Get YourP” in the search bar and selecting the “Get YourPointerHere” option that would appear in the list.

FYI C++ pointers are called “references” in Blueprints. i.e if you make a BP variable of type Actor, while you are in the dropdown menu you will have 4 available options for the actor class, one of which is “Actor Reference”.

Blueprints have no pointers.
A “Reference” is a textual path to an UObject in Blueprints; this way the “pointer” can be copied around without major problems (Because Blueprints pretty much pass everything as a copy).

I.e: “PIE_SomeId:MapName.ActorName_C_xx.CompName_C_xx”
would be a Blueprint “pointer” to a Component.

So what if I had spawn volume that spawned hundreds of actors? Should I keep all spawned actors in array?

So what if I had spawn volume that spawned hundreds of actors? Should I keep all spawned actors in array?