Why can't uobjects spawn actors?
I'm creating a spell system. My spells, which are UObjects, are in charge of keeping the spell data - like cast speed and the next time it's able to cast - but they're also in charge of executing what the actual spell does - like creating actors. For example, there would be a spell called "Spell_Fireball" which creates an "A_Fireball". Pretty standard, right?
The problem is that I can't create actors from UObjects. At least, not in blueprints. This is a problem because, well, my spells now cannot interact with the world. I'm forced to either make my spells actors, which sounds awful in so many ways, or couple them with an actor which can create actors. Both sound like extreme code smells, so I'm not sure what exactly to do.
If someone can shed some light on why it's like this, and how to do it differently without the two ways I've already mentioned, I would be very grateful. Not to mention UObjects are not replicated by default, so doing this in a replicated environment will give its own headaches, but one problem at a time!
UObjects dont have a World Context. In C++ you can simply overwrite GetWorld() and telling it where to get UWorld from (many can exist at once) and everything will work like in a regular BP.
If you can´t do C++ than simply pass some Blueprint along that does the Spawning for you and returns the Spawned Object.
answered May 24 '17 at 05:19 PM
"The problem is that I can't create actors from UObjects. At least, not in blueprints. This is a problem because, well, my spells now cannot interact with the world."
The chief difference between AActors and UObjects is that AActors can interact with the world and UObjects cannot. This makes UObjects significantly lighter than AActors, which is their advantage.
Nachtmahr is on the right track; you will need to give your spells access to an AActor that can do the spawning for you. But as I read the example provided, you're trying to spawn that intermediate AActor BP from your spell UObject, which you can't do (if you could spawn AActors you wouldn't need the intermediary).
Hard coupling your spells with a specific actor class is probably not the best way to do this; then you'd need all AActors that use your spells to have access to that specific AActor class, or be a child of that AActor class. What will probably be your best bet is an interface that your spells can use to trigger a spawn from an outside AActor.
When casting a spell that requires a spawn, you can require an AActor that implements the interface to be passed in to the spell as the SpawningActor.
When the spell is cast, if the SpawningActor isn't available within your UObject the spell will fail silently; so you'll want to verify the passed AActor implements the interface before continuing execution (DoesImplementInterface). With the interface verified, the spell UObject can call a method defined in the interface from the given AActor.
Define a "SpawnSpellEffect" method in your interface (use whatever name you like); you will need to pass in the class to spawn as an argument, and probably want to pass in a location, and maybe some other parameters - that's up to you. I'd suggest having the SpawnSpellEffect method return the newly spawned AActor, that way your spell can continue manipulating it as necessary.
Any AActor (or object) that casts a spell will need to be able to pass in a valid SpawningActor (which could be itself).
Any AActor you want to be used as a SpawningActor would need to implement your interface, and define the corresponding method(s). Each of your SpawningActor(s) can also define their own ruleset for success and failure of spawning spells.
answered May 24 '17 at 08:07 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here