How to cast to spawning actor?
It's probably something super easy or just not the right the way I'm trying to do this, but I'm spawning an actor from an array, which means that I do not know which actor with which variables I get.
How do I cast to the spawned actor to get variables that are saved in that BP? The problem is probably that I didn't really get the concept of casting or so, so please educate me on that one, if that is the issue.
Every help is really appreciated, thanks in advance for any helpful hints you have :)
This is the script, I'd like to get variables specific to the blueprint which was spawned.
asked Jan 26 '18 at 09:50 PM in Blueprint Scripting
There's no elegant solution but you have a options.
First, as ThompsonN13 said, you have the standard chaining of casts. Cast the result to your first class and connect the "cast failed" pin to another cast with the second class, and so on, chaining them together. It isn't ideal but it's quick and dirty. It should be fine if you only have a few classes to check.
The other option is to add a SwitchOnInt node after the spawn, feeding it the same index that you use in your Get. Then you can get an execution pin for each class in the array. Give each execution pin its own cast-to for the appropriate class (that matches the class in the array at that index). This is the better way, IMHO.
It'd be nice to have a cast-to node that took in a class to cast the input object to rather than each cast being its own node. That would be really sweet. But there isn't one now so you have to go with a less ideal approach.
It's impossible even on conceptional level, because even if you would would able cast dynamically on run time you ferther code would need to be designed to casted type, so either cast might end up invalid to your code if you would use specific functions or such cast will be pointless if you didn't.
You need to create base class (in you case i assume class for buildings, so make blueprint name Building) in blueprints with common variables, functions and events and all other object need to base from it. this way all your building classes will have same functions, vareables and events and will be able to be casted to same common type. Class inherence is one of most basic concept of objective programming which UE4 (and most games now days) heavily use. For same reason you can use same nodes in all actors, common nodes to all pawns and xharaxters because they based on common class they inherent from. When you create blueprint you selecting not type of blueprint which lot of beginners think you picking a class from which blueprint will be basing on and you can select other blueprints as a base because blueprint = class same as in C++.
There also interfaces which allows to relate unrelated classes with under common interface, if there 2 or more classes that have same functionality but diffrent bases classes and can't be casted to common type, interfaces allows you to do that
Long time ago i made video explaining how classes work, because classes are not display as a tree anymore but as asset people get confused at beginning how UE4 code works.
If you really intent to use specific functions to specific building, you might consider to use switch node or select node based on class of the object you trying to process.
Follow this question
Once you sign in you will be able to subscribe for any updates here