Cast to blueprint vs Get all of class > cast to blueprint

Last night I finally managed to cast to another blueprint correctly.

My level has an actor, which spawns instances of another actor. I was trying to cast to the first actor but failing miserably.

I had been attempting to use something like

action > Cast to first BP > variable

but it didn’t work until I did:

action > get all from class > get (array) > Cast to first BP > variable

This worked. I’m not sure why though.

Can anyone tell me why the first did not work when the second did?

An image of your nodes would be really helpful to narrow down the problem. I have problems to understand what you’re doing.

Is the another spawned actor a child class of the former actor? Casting (or type conversion) doesn’t work arbitrarily, the types need to be related.

I can get it later, unable to do so right now (and the BP is messy af)

Neither blueprints/actors are children. The first BP is the only one that exists (it manages other actors) and the second is an instance of a blueprint, but all instances will be treated the same.

First BP exists and on runtime spawns the second BP.

You are trying to cast the actor you spawned to the class of the actor you spawned it in? This will only work if both actors are of the same class or if the class of the spawned actor is a child class of the other one. I think you misunderstood what typecasting does.
Assume the following: You have 1 class ‘Human’. And 2 child classes of the class ‘Human’: ‘Woman’ and ‘Man’. The child class ‘Woman’ does have the boolean variable ‘IsPregnant’. Now you store all women and men together in an array of type ‘Human’. When you want to get all humans that are pregnant you cannot call the variable ‘IsPregnant’ on each element of the array because that variable doesn’t exist in the class ‘Human’. What you need to do is to cast each element of the array to the class ‘Woman’. For all men this cast will fail. On all women you can now access the variable ‘IsPregnant’. I hope this helps you to understand what casts are made for.

I think what you want instead is the node GetInstigator inside your spawned actor. So you get a reference to the actor where you spawned the other one.

Casting is part of polymorphism. You can keep a list of a BaseClass, in this case Actor, even though each object in the list might be a different specific type class, but they all would somewhere in their hierarchy extend from Actor. Like if you have different types of Characters, each with a different type of Mesh and Behaviors, you could keep them all together in an Array of Characters because they all extend from it. You could not put a Camera into that Array of Characters. But, if they are really SubClassses of Actor, such as Pawn, or YourPawn then the Cast will succeed. It’s just part of Object Oriented Programming and it’s Implementation in C++ which Blueprints basically extend from. You can use the Cast as a test as well, if you only want the “RedCharacters” in the list you can attempt a cast while looping through the array.

You may be confused because the spawn node returns an Actor reference to the newly spawned Actor Subclass. You can pass it around as an Actor, or Cast it to the Class it was constructed as. You could also just add it to an Array your manager class has and anytime you want to do stuff to all the Actors it’s supposed to manage loop through the Array. The GetAllActorsOfClass will get *All of them, not just the ones that your manager class made. It will cause bugs for you when you have a couple manager guys and wonder why they are calling functions or giving orders to each others units and not just their own.

Thank you both. This is helpful. The goal that I was trying to achieve was to get the size of the second actor, which is predermined before it is spawned.

If the first actor defines the object size boundsX and boundsY for all actors that it be responsible for spawning, how best would I get that value from the blueprint of another, completely different class? In a similar instance, if the first actor (spawner) needs to spawn something of a different value, what is the best way to do this? I had considered using an advanced function, but apparently they cannot creat

Every time I have looked for some form of ‘global’ variable, i’ve found a link to casting. Is there a better way to do this?

Global variables sometimes might use the type ‘Actor’ as the variable type. That way you can set the value of that variable to any instance of an actor class or a child class (any child class and any of their child classes and so on…).
If you know you have stored (for example) a pawn in the global variable you can then cast the global actor variable value to the pawn type. That will work. But if the global variable didn’t store a pawn type value the cast will fail.

You can spawn from a function. From then node, cast the actor after it has spawned to your class and then set whatever variables you want on it.

Using a global variable is probably a very bad way to do this and you don’t need it. Casting has nothing to do with Global variables, so just forget about that. You always will need to Cast, it’s just the nature of any program that is using Object Oriented Programming as it’s paragin, it’s just part of it and nothing unique to Unreal Engine.


To make sure we don’t completely confuse the OP about typecasting I want to mention that the cast in your image isn’t necessary and the compiler should warn you because of that. Beside that this is a good way to solve the OP’s problem.

I thought it was, for some reason I thought it returned an Actor and so I have always done a cast there, for the last couple years actually.

Glad I posted that, I learned something.

I’ve noticed that I still get error warnings, but I now understand how to use these. Thanks for the help.