What is the proper way to add a blueprint object reference in a C++ structure?

In my project, which is full BluePrints, I’ve decided to move all my BP Structs and Enums to C++ as I’ve read it gives them more stability.

I’m new to C++ but luckly, the moving has gone smoothly with the declarations normal variables until i got to BP Actors object references.

My old Structs have object reference of BP actors so I’m unsure on how to it declare them in the C++ Stucts, and I haven’t found a clear answer from the internet.

Do you guys have any leads or examples?

you must get the class of the BP FROM the BP. that means you must put the class of the blueprint you want to in the C++ world in a variable that you can edit in the blueprint:

UPROPERTY(EditDefaultsOnly)
TSubclassOf<AActor> myBlueprintClass;

adding EditDefaultsOnly allows you to have the variable exposed in the defaults
of the blueprint and then select the class from there

then you can make a variable of that kind

myBlueprintClass* theVariable;
2 Likes

Thanks a lot eldany!

2 Likes

The TSubClassOF works but when I try to make the variable It throws me an error.

Not sure what went wrong.

That will not work.

When you declare a property, you need to specify a class so the compiler understands what you want to store in that variable. In the example, BP_Unit is a variable that can keep information about a class but is NOT a class. The compiler can’t know what you want to store.

What you need is a simple pointer that supports the class of your blueprint, which means a PARENT class. Probably AActor* would work, or UObject if you are working with other than actors. If your blueprint inherits from a c++ class, you can use that class.

Of course, you can’t directly access the functions or properties created in the blueprint level from a c++ pointer, but you can call them by name function if you need.