I’m getting into blueprints in UE4 from a programming background and things are going great, but there’s one thing I can’t seem to figure out! I’m also surprised I can’t find anyone else asking this question, so I must have a mental block somewhere.
How do I create an instance of a non-actor blueprint on runtime!
I have multiple uses for this such as:
Spawning a new item on the map and dynamically give it an abstract “StatsBonus” blueprint instance with a random property.
A player picks up a bag and has his abstract “Inventory” blueprint instance reference replaced with a new Inventory instance that has a random number of slots
A character is cursed and receives a newly created random “Debuff” blueprint instance that is then added to his list of active debuffs.
A character learns a new spell and has a new “Spell” blueprint instance added to his spellbook
These things can be done by making all of the above blueprints into “actors” so they can be “spawned” but that can’t be the correct way to do it, as they basically become invisible objects with some arbitrary position.
Another solution could be to pre-create all these blueprints, but if they’re supposed to all have their own properties/variables then that becomes impossible
So what am I missing? Am I simply too brainwashed by OOP?
Use blueprint structures - this solves most of the issues, but to add custom behavior to the structure, you would have to do something like having a blueprint with a number of functions that all take the struct as a parameter and then does something.
AddChildActorComponent - Could kind of give a meaningful semantic to the actor solution, but still rather awkward
You can create object based blueprints with 's plugin, you can find it somewhere in the blueprint visual scripting part of the forum.
The problem with this is that you can’t subclass blueprints based on Object and you can’t reparent them to Actor or any other Actor-based class.
I wanted to use Object as a base class instead of Actor but that’s currently not really possible i think. What i want to do could also be done with structures in c++ but not in blueprints because blueprint structures are very limited.
Note that majority of UE games uses Actor as base for non physical objects, even in default UE4 classes you can see that with classes like GameMode, Controllers etc. everything related to game usually end up as actor, it was like that since very first Unreal (since class structure didn’t really changed since then). I don’t know why is that, but my guess is that Actor is made for all gameplay elements not only physical objects and it closely tied with world context (as i know people have problem with using () outside Actor for example). Thats why Epic don’t see issue by not blueprinting object class
“blueprint is for prototyping, and c++ is for optimization”
If that were the case, Unreal was doomed, because the documentation for C++ is so bad, that hardly anybody is able to write a game in C++. That’s why so many people use blueprint
in many cases, iterative prototyping in a graph editor is better than optimized, hard coded, slowly compiled, written in stone, walls of text full of references to other walls of text.
I didn’t mean that you should convert all of your blueprints to c++, because plenty of things don’t require that extra bit of optimization, and plenty of things benefit from the fast prototyping that blueprint allows.
also, this is an old question, and the answer has changed alot. now you can make structs in blueprint, you can create objects in blueprint, which are cheaper than actors, you can use AssetIDs to asynchronously load assets when you need them, you can use datatables to import spreadsheet data into the engine, treating each row as a struct that can be accessed by name and used to initialize an actor.
when i wrote that answer, none of those improvements existed. so now blueprint can do a ton of stuff that used to require c++, and it is getting better every release. in 4.10, we may even get a button that converts blueprints to c++ automatically, so at that point, the question of converting some code to c++ will have a completely different set of pros and cons.