I have a blueprint with parent class Actor (BombComponent in the attached image). I can get its Class object with a Get Class call, but it returns an Object Class. I want to cast this to an Actor Class so I can use that with a SpawnActor node to spawn it (SpawnActor only accepts Actor Class, not Object Class).
This cast always fails. What am I misunderstanding?
Yes I am. When I select the BP in the SpawnActor node, everything spawns fine. However, I wish to be able to dynamically select an actor class to spawn, instead of hardcoding it into the SpawnActor node, which has lead to my current dilemma.
The issue is with how you’re getting the class, not the cast itself. I found a hokey work around, I don’t understand what’s wrong with using “Get Class” node, but there’s a “Select Class” node I just configured to always return the A value.
Yea, I don’t see what’s wrong with using GetClass the way you are doing it, but every example I’ve found of GetClass always has an input, not selecting the class in the dropdown. Perhaps that is a bug.
“Dynamically select an actor class to spawn” , do you mean in-game during runtime ?If you can provide me with a little more information on what exactly it is you are trying to accomplish I might be able to provide you with a solution.Hope to hear from you.
My project is basically to re-create this bomb defusing game (simply as a study project to get my feet wet in UE4/VR), where a bomb has a number of components that each need to be disabled in order to defuse the bomb before the timer runs out. Each component is effectively a mini puzzle, and exists as a 3d model (actor) in the scene.
So, I’m making a bunch (right now I think I have 6 planned) of different components. These are blueprints that inherit from my Bomb Component blueprint, which inherits from Actor (see the picture in my initial question). I’m currently working on the system that randomly selects and spawns X amount of these components, depending on the difficulty setting (more difficult = more components).
Since the SpawnActor node requires an Actor Class object to determine what to spawn (see the 2nd picture, just above this post), I figured I could make an array of Actor Class objects (so not Actor Objects) that contain all of the components, and during runtime randomly select from that array to pick the component to spawn. This means every time the bomb has a random/different set of components, which should improve replayability.
However, it appears the only way in UE4 to get a reference to the Actor Class is with the Get Class node (picture 1), which returns an Object Class and not an Actor Class. The SpawnActor node requires an Actor Class and that is why I need to be able to cast Object Class to Actor Class - so I can get a reference to my blueprint to dynamically spawn a randomly chosen component into my scene at runtime.
Furroy pointed out above that it seems Get Class isn’t working as one would expect but doesn’t know why, and it might also be that UE 4.7 has an additional mechanic to obtain a Class reference. I’m still on 4.6.1 though.
Thanks for the detailed explanation , before I saw your reply I created a basic example of a “spawn system” which is not “hard coded” as you described above.Which I will post , hopefully you find it interesting ,its not a “how-to” but more just a basic explanation , as there is almost always more than one way of doing something , depending on your needs.I’m not sure how experienced you are with Blueprint Scripting , so forgive me If my explanation is too/less informative.
I’ll read you’re explanation through another time and see If I can provide you with an additional solution/explanation.
System created was tested and works as expected.
Just another important question.You are referring to components of “A Bomb”(yours in this case) and not components in Blueprints that makes up a Blueprint right?
Hope this sheds a little more light.
Reading through your explanation again , is occurs to me (might be wrong) , you want to “get” your Bomb Component(which is a parent actor) but you want to spawn (child components) of the parent(BombComponent) ?
I ran into the same issue, and ended up needing to just create a temporary variable for the Actor Class I wanted to select and using a Set node with the drop down selected, instead of the drop down on the Get Class node. Ultimately simpler from a blueprint point of view anyway, since it reduces the number of nodes (no casting required).
Looks like @Ockertvs used a similar solution, although he’s just using a true/false branch instead of a Switch To (+ Enum in my case) node to select from a larger list of Actor Classes.