'Cast to' IO data types

Hey, UE4 rookie here (mildly put) - I was watching this videowww…com/watch?v=xu1a9jFZc7g&index=9&list=PLZlv_N0_O1gZg3dTMetmsfm_s4lb4-Tg0 which cover a ‘cast to’ node. First of all, what does this actually do? It seems it is used as a ‘if actor is of type X’ but the name ‘cast’ just makes me think of raycasting of some sort…

Anyway, in the video he puts a ‘cast to’ node between a ForEachLoop and a Move Up custom event. If this ‘cast to’ node is just a safe way to check if the actors inside the array that gets passed to the ForEachLoop it shouldn’t necessarily be needed, but it is as the custom event’s ‘Target’ input can’t take a ForEachLoop’s output (Array Element).
It seems ‘cast to’ changes the input data type to something else…

What is actually going on here? ‘Cast to’ doesn’t have a tooltip (yet) btw either.

This’ll be my first post here btw so should really say big thanks to the dev team for UE4, learning it has been fun so far!

Hello!

That’s rather complex to explain for me, so take a look at this: http://en.wikipedia.org/wiki/Type_conversion
This is what the cast node does, it gets an object of some class and outputs the same object but now it is of the data type specified in the node.

‘Cast to’ is ensuring that a specific data type is used at that point. If for example instead of checking the data type (for failure states) at every step of a calculation, you would cast it to the data type you wanted to use specifically first.

I’m not sure if you have much of a programming background, but this is pretty common - very much so indeed. An easy example would be where you take input as a string, and then implicitly cast the result to an integer before doing a calculation with it (obviously in this case the input would be numbers), you don’t want to deal with the conversion repeatedly, and you don’t want to clutter up whatever your math is doing checking for types and conversions etc so you explicitly cast to the desired datatype beforehand.

If you meant more as to literally what happens when you ‘Cast to’ something, it’s so that instead of handling different data types in every single function, you would make a cast function that you first would run everything through, all it does is ensure proper transitions between data types (int to float, int to double, int to string, whatever). So you could have Variable B which is a string with the value of “1”, and then make a new Variable A as an integer and explicitly CAST B to type integer and store the result as A.

That explains it pretty well, thanks both! I’ve done some coding (Python mainly as of late which is obviously forgiving when it comes to this) and I guess ‘cast to’ was a term I haven’t come across yet.

So let’s see if I got this right, in this case the input into ‘cast to’ is an array of objects (4 mins into the video) which gets cast to ‘shrubmove’ which is a class blueprint, and the class blueprint data type is needed for the custom event which is piped into the end?

Aren’t the objects in that array already of the blueprint class ‘shrubmove’ though? To me it kind of still seems the array contains the correct data type already, albeit in an array. I mean he catches all actors the type ‘shrubmove’, then converts them into type ‘shrubmove’, both being considered objects ie no conversion was made?

Thanks for the help again, I’ll get the hang of this shortly I’m sure! :slight_smile:

Honestly I don’t know why he’s doing it specifically at that point in that video. Hopefully someone with more blueprint knowledge (maybe , or ) will glance at it and tell you exactly what the video is accomplishing and why it was done that way. Sorry buddy.

Ok, thanks for looking into it anyway, at least I know what ‘cast to’ does generally speaking. :slight_smile:

Hi ,

What the video is showing is how to access multiples of the same class within a blueprint at this juncture. He is using the cast-to node to ensure that the overlap event only affects the shrubs and no other actors. This way, when the player goes over the overlap blueprint, it does not lift, for instance, the player, and instead only lifts those objects that are specifically shrubs, but not a single shrub.

Think of casting like television actors. We can use Firefly for an example: Nathan Fillion (An Actor) is a parent class over the acting role (Pawn) of Malcolm Reynolds (A Character) and can therefore Cast To Malcolm Reynolds. While Nathan Fillion does not have a Gun, Malcolm Reynolds does and he can then easily cast to Malcolm Reynolds to get it.

Kaylee is not directly connected to Malcolm Reynolds, so she would have to find a way to reference Malcolm Reynolds. Then she could use that reference to Cast To Malcolm Reynolds and tell him to use the Gun. Now, let’s say we have multiple Malcolm Reynolds in the same environment, but Kaylee wanted a specific one to shoot the gun. That’s where the ForEachLoop comes in. It checks the array, casting each to Malcolm Reynolds, which gives Kaylee the access she needs to get a specific (for the example lets say Malcolm number 2 in the array) Mal to fire. Even further, the Cast To on an array of all actors in the show would allow her to specifically target Mal instead of River or Sheperd Book.

The TL: DR or less nerdy version: Any actor can cast to any other actor as long as they have the proper connections to do so. You use the Cast To node in a ForEachLoop to check for things such as whether or not the actor is cast to a specific class and/or to get specific instances of the class within an array, that way you can effect either all of them or just one.

Cheers for helping out here, and I love the nerdy description. :slight_smile: I must admit I still don’t fully get it though… the GetAllActorsOfClass collects an array of actors of class ShrubMove, then two nodes later he checks that those actors indeed are of class ShrubMove… which seems redundant as he only collected the actors of that class?

I know there’s some sort of data conversion going on in the cast to node as the MoveUp custom event won’t accept input straight from the for each loop…

Pardon my slow brain for a minute here, I’d really like to fully understand how Unreal works.

Edit: The way I can kind of make sense of this is if the actors don’t have access to their blueprint class’ custom event, and the ‘cast to’ is casting the actors to their blueprint class which contains the events (ie assuming an actor and its blueprint class aren’t the same). When creating a blueprint reference variable to an actor you can get custom events from that actor without conversion so that doesn’t make sense to me either. I must be missing something fundamental I fear.

With that it is less that it is checking if they are shrub move so much as which shrubmove they are specifically. The cast to is basically sorting the information and making sure that each actor in the array (or a specific one) gets the information passed to it. Without the cast the information sits stagnant as the blueprints are not interacting with one another.

Aah… these murky waters are beginning to clear… so the array isn’t a live reference to the actors within it? I thought the custom event call would force the blueprints to interact.

Much appreciated replies.

Unfortunately without the cast to the blueprints or a specific actor the blueprint doing the casting doesn’t know which actors to interact with. It gets the array and fires the function, but because it doesn’t know which actors to send it to none of them get the output if that makes sense. For the example, he wants all of the bushes to move upwards, which means he has to tell them all to do so. Without the cast the overlap blueprint is effectively shouting “hey you bushes, move up” and the bushes respond with, “which one of us?”. Until the know which ones none of them will actually move.

I’m not sure why it wouldn’t know which actor to interact with, it iterates through the array calling each actor of said class one at a time?

Actually without the cast to the foreachloop output (single actor) can’t even connect to the custom event node, indicating there’s some data conversion going on(?). A variable reference to a single actor can connect fine to a custom event. To me it seems they both are the same.

I think I’m just going to move on thinking arrays will need this “conversion”, I can’t say I fully get it but rest assured I will once I dig in more. :slight_smile: I now know actor objects in an array can’t be accessed without a ‘cast to’ node which likely would’ve taken me a long time to figure out the hard way! :slight_smile: