Where did "Cast" come from?

I understand that this is an old part of the course, but I’ve been through the old sections before and up until now I have never seen this function or whatever it is been used or explained in the past before. I’m not sure if I just can’t remember, but I feel like I’ve missed out on something here, accidentally skipping over something maybe.

Any clarification on what this is or if I genuinely just missed where this was explained would be nice to know :slight_smile:

Assuming you’re talking about type casting: a simple explanation is that it’s a mechanism that allows for one data type to be interpreted as a different data type. As you might expect, there’s plenty of language rules surrounding what/when/how/where/why it can be used. I’d recommend googling “C++ type casting” if you want an in-depth explanation, or if you have a decent C++ programming book, it probably covers the topic in a reasonable level of detail.

If you’re not talking about type casting, then the previous explanation is irrelevant and can safely be ignored. :slight_smile:

-CA

Hey thanks for the explanation, this is the line of code I was specifically confused about:
image
What this function returns is what really confuses me, as Ben shows this out of nowhere and doesn’t actually explain what Cast is. I’m not sure if it’s something within Unreal or C++.

I’m not sure if my googling skills are awful but I tried looking up what you said and I couldn’t find anything related to this line of code, so I assume this is something different as to what you were explaining, but thanks anyway :slight_smile:

Ahh ok, now I see what you’re talking about. That IS a type cast operation, however, I think it’s a wrapper for underlying language type casting. Essentially what that syntax says is “return the value of the function GetPawn() interpreted as a pointer to the type ATank”. GetPawn() returns a pointer to an AController, which I suspect is the parent class of ATank. The cast is there to explicitly perform the conversion between the types. I also suspect there’s likely more happening under the hood in Cast<> as well, but I’m not familiar enough with it to provide any concrete information.

There’s also the trailing ‘const’ at the end, which indicates that one cannot change any of the member data. More info on that here: https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-function-declaration-of-a-class

Some additional info on Cast<> here: https://forums.unrealengine.com/development-discussion/c-gameplay-programming/40599-casting-c-syntax-and-ue-syntax
The 4th post down looks to have a decent explanation.

This is some general C++ type casting information: http://www.cplusplus.com/doc/oldtutorial/typecasting/

Hope that helps! :smiley:

-CA

Privacy & Terms