How to cast to spawning actor?

I dont know of any easy way to accomplish what your looking for. if the variables were going to be the same (same name(health, ammo, etc) different values.across all the items on your array then it would be easy to setup a parent class then just cast to that. there is also a get default values node that you can look into but i dont think it would work in this case. Below i also added a basic explanation of casting that i had from another post.

"The most basic thing to know about casting is that its just a comparison. it takes an item and says is this item like the thing im checking for if it is then true if not then it fails . so for example lets say instead of checking if its thirdpersoncharacter your dealing with cars. if your looking to see if the item you give is a car then a ford mustang, a geo metro, and a model-A will all come true, but if you feed the cast (object in) a 747 airplane then it would fail since its not a car. its all about inheritance a mustang would be a child of car and inherit the attributes of car (basic shape, 4 wheels, engine). the reverse is not true though if you were casting for a mustang and you fed in car it should fail. All mustangs are cars but not all cars are mustangs. " -Explanation i gave in another post

Hey,

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 :slight_smile:

This is the script, I’d like to get variables specific to the blueprint which was spawned.

There’s no elegant solution but you have a options.

First, as 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.

2s

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.

thanks for the shoutout but i really wasnt getting at chaining cast nodes. i actually didn’t even think about that one.

Woopsie. :slight_smile:

Thank you very much for your answer, it’s probably the most right one,
Especially since the SwitchOnInt is more or less exactly what I’m looking for.
But how much are “a few” to you?
Do for example 100 still count as few?
Or should I try and limit it to 10?
Are a 1000 still ok? Since that’s easily possible to achieve with what I’m planning to use this for.
If not, I’d still be able to find a workaround, but I need to know beforehand ^^

what kind of variables are you looking to store and reference?

I probably misformulated that one… it’s not so much that they are entirely different variables, but different amounts of variables.

For the buildings I’m having different costs of different materials and didn’t want to put every single material in existence in the game in every building and set all not needed materials to zero, just so I can have 2-5 materials have a cost value.

So I just want to have these 2-5 variables in the buildings with an material index and the needed amount, I just can’t seem to think of an cost-efficient way to make that happen.

Hard to tell from the code but it looks like SwitchOnInt acts a lot like a switch statement in C++, so it’s not too bad. But still, it’s going through every value and comparing them one at a time so it’s not ideal.

For something super-fast you’d want to just index an array. Then there would be no performance hit for adding more pins. But it seems that would require some custom coding in C++ to fashion your own node and it doesn’t look like it would be especially easy. You need to interface with the editor and the compiler.

it sounds like for each building you want to have a cost to build it like for one itd be 20 brick and another would be 50 wood, that sort of thing. if thats the cast i would create a parent class with two variables; buildingmaterial and cost. in this way you could set the buildingmaterial to whatever you like in the children (brick, stone, metal) and it would have an associated cost. you could even have multiple sets of those variables per actor if you wanted the build cost to be multiple types of materials.