L117 Cast<ATank>

In the code

ATank* ATankPlayerController::GetControlledTank() const
    return Cast<ATank>(GetPawn());

what does Cast<ATank> do?

1 Like

It allows the program to see the pawn as an ATank. If you don’t cast, the program will only know the object as a pawn (pawn is a parent class of ATank), and won’t be able to use the features that are defined in ATank. Inheritance can be a little bit of a tricky subject to understand at first, but at the level that UE4 uses it it’s important to understand.

I had the same question, Cast is a template defined by the engine, not so sure about how deep it goes but it must be choosing the right cast for this, I was wondering why we are not using std::static_cast or std::dynamic_cast since naked casting is a big flaw.

If somebody knows what this “Cast” exactly does please let me know :slight_smile:

Would make sense to use engine specific tools rather than the Standard Library when using the engine. Though I can’t answer on what exactly it does.

C++ is really weird. What does the const do ? :scream:

You can use Visual Studio to navigate source code.
“Cast” is a templated function that only delegates to TCastImpl:

template <typename To, typename From>
FORCEINLINE To* Cast(From* Src)
	return TCastImpl<From, To>::DoCast(Src);

template <typename From, typename To, ECastType CastType = TGetCastType<From, To>::Value>
struct TCastImpl
	// This is the cast flags implementation
	FORCEINLINE static To* DoCast( UObject* Src )
		return Src && Src->GetClass()->HasAnyCastFlag(TCastFlags<To>::Value) ? (To*)Src : nullptr;


FORCEINLINE bool HasAnyCastFlag(EClassCastFlags FlagToCheck) const
	return (ClassCastFlags&FlagToCheck) != 0;

So, I guess that Unreal is using its own reflection mechanism in order to to check that the cast is correct before doing it.

In this particular case (because AFAIK RTTI is not disabled at compile time) the same can be obtained with a dynamic_cast<>.

I’m not sure but we used auto as player tank .then we want to to return player tank.but it is not specified type of player tank so we need cast.also I read in Unreal forum cast means treat as we dictate it we want to player tank treat as A tank
:slight_smile: smiley:

also I must add if we change code like this:

ATank* ATankAIController::GetPlayerTank() const
//get pawn but we cant use Atank

ATank* PlayerTank=GetWorld()->GetFirstPlayerController()->GetPawn();
if (!PlayerTank) { return nullptr; }
else {
	return PlayerTank;

we get this error:
error C2440: ‘initializing’: cannot convert from ‘APawn *’ to ‘ATank *’
and it is obvious why we use cast.

makes it so that the method or variable is set to a value, and then doesnt ever change. for example:

const float pi = 3.14
no pi can never be changed from the value 3.14

I was wondering about this. What is this ? Takes some constant and returns a pointer to an ATank or what ?

Ah wait, it does return an ATank but what does the const mean here ? The pointer is constant or what ?

It means that nowhere in class code you can change this Tank values with this variable. Const just make sure that the variable is read only. It’s good if you need a reference to the variable, but you don’t want the class to modify the tank.

Privacy & Terms