How can i Cast a variable to a type selected at runtime?

Hi, i need a way to cast a variable of type Actor Reference(not important) to a type of class, currently the API is super rigid i cant cast to anything dynamic, by dynamic i mean a type of BP that already exists in the project but that is decided at runtime.
EX:
What i want:
Spawned Actor Ref > Cast to (runtime selected type from a predefined list of existing BP classes) > use result.

What we have:
Spawned Actor Ref > Cast to [i have to tell exactly what type on the spot] > use result; BUT this could be a weapon or sub weapon BPclass that is selected at runtime not at coding time.

Is there any BP workaround or C++ solution where i can plug in dynamically an existing type to cast a reference to it?this would make cast failed probably, pointless since the spawned type class bp is the same inserted into cast to node.

Im stuck without this. Im sure there must be an even longer way around all this rigidity of the API but i do not know how to do, i literally spend 90% of the time fighting the API that wont let me do anything, i can barely prototype.
Thanks.

![alt text] (https://s1.postimg.org/5huawdtmlb/problem.png)

1 Like

I’m adding my two Zimbabwean dollars as a comment instead of an answer because this is just my silly unsubstantiated opinion, but how I understand the framework is that the following points are important to take note of:

  • It’s quite an opinionated framework (pun intended)
  • Blueprints should (generally) not be seen as extended classes (even though it provides designers with amazing flexibility to create things from scratch). Instead for serious development it should practically be seen as class instances with customisable parameterisations of parent class properties - in effect having access to a version of the parent class and its constructor that you can save down as a usable object
  • There is a heavy kurtosis favouring parent classes in design (which is actually in line with proper OOP/D (check out the implementation of movement-related features as an example)
  • Unreal comes from “true” (back in the day) OOP - the framework was not created at a time when we were used to the simplicity of languages, frameworks & libraries like C#/.Net/Python/Ruby/anything more accessible.

Taking the above into account and pretending I’m not talking out of my ■■■, I would suggest rethinking your design. You are asking for a solution to a problem which shouldn’t exist if you follow the design principles shown in the engine (as debatable as their validity might be with new C++ feaures). In short, to take an example of how I had to implement weapon system is that my base Weapon class has a reload function which doesn’t apply in any way to e.g. melee weapons, and in those melee weapons I just leave the implementation blank (as in the base class) because that’s easier than having to do casts to figure out whether it’s a “reloadable” weapon (Interfaces we can discuss as another topic). I’m really sorry if this isn’t the answer you’re looking for, but my point of view is simply trying to practically deal with the framework (after being raised on C#):

Part 2)
(continued) Embrace looking at the patterns followed in the engine/framework (however strange and/or questionable) and it will serve you very well, even though it might be more cumbersome than you’re used to - it is easier than fighting against existing design.

P.S. Did you ever use UnrealScript in UE3? I don’t care how many principles that broke, but being in control of the call to Super during the constructor was absolutely amazing.

You can’t cast dynamically, it might not look that way in blueprints but cast is not dynamic thing, same as declaring type of variables this is only a agreement with compiler that you sure that this object is of that class and you changing type of varable from this point and based on that compiler properly generates code. So cast happens when you compile the code not on runtime, in blueprint it only check type, but change of type happens on the pin of the graph, you already can see that with SpawnActor which won’t cast you to type of class you inputing to pin for exact same reason and it also why you want to cast.

I can’t imagine that you gonna make a code that will utilize functionality of any type possible, there no way you would be able to do that, there always be common ground. You planing to use some functions so oyu should cast to most common type that have those functions, if you cant’ use class check and cast to that type needed.

I don’t know why you want to do, but maybe what you looking for are interfaces. Interface is like sub-type of class which can relate classes that are not related in class tree, but they have common functions and varables.

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Types/Interface/UsingInterfaces/

So make interface with functions you need and make all needed actor classes implement them so you can make code common to them.

1 Like

Its ok any piece of information is usefull and it adds up to understanding the framework better.
Altough, im only beginner in C++ so i do not understand advanced C++ in the API and its design so i have to use BP’s for the time being.
If the engine wouldnt force me to do A,B,C,D before i can do E would be a lot easier.
Example i have my MechRobot basics coded in BP, but now i want to let AI control him but i have to use AIController, i made my AIController like in official tutorial, using AIMoveTo but thats no good, AI moveTo ignores my custom movement code inside MechRobot BP, and all other custom code, since i used direct Input events inside MechBP the AI controller cant use them so i have to refactor/recode everything inside MechBP, probably to Interface functions and then code all the Input inside PlayerController BP, and AIController will call these functions aswell when it decides to move/shoot etc.

I have no scope of whats ahead, i dont know how other things like networking or behaviour trees will affect this design, i might find out its no good and i have to start from scratch again.
Im trying not to fight with the engine anymore, still the progress is terribly slow because i constantly find my design not suitable/not modular.

In the end all i want is easy modular BP’s with custom behaviour and easy to access functionality but the casting stands in my way at every step, i can easily get a reference to my dynamically spawned objects but in order to access it you need to hardcode cast to a specific type and its a dead end design.
Using interfaces is somewhat workable with since i can use the reference i have to call an InterfaceMessage without casting but it makes me work harder for no reason when i already have the object reference i should be able to access all its functionality.
Thanks.

The problem arises when i already have a reference to an actor that i spawned or setup on begin play and when that actor could be of different types, think multiple weapon types BP’s that share same code but i do not know the type at spawn because the player might choose different wepaon etc. If i need to call a function on that actor that all weapon that all weapons have but i do know know the exact type i cant cast to BP_Sword and when the player will have BP_Dagger cast will fail.
At least that was the idea.
Ive used interfaces but i think im not thinking creatively enough, coming from unity’s C# where i can access anything however i want i feel the rigidity in the API every other minute i code in BP.
I tough maybe BP’s are some form of dynamic scripting language but after researching a little bit more it apparently uses C++ so i doubt my request is possible.
I

Then as i said make base of all the weapons which contain common function and common behavior code, if your your weapons gonna be based from that class, the cast will never fail and you able to use common functions, you need to use specific function for more specific type then class check and cast and call. C# as i remember also have types that you need to cast to, JavaScript for example don’t have that and it’s criticized by language nerds for that same goes with PHP, never coded for Unity so i not sure how there class structure look like,