What does the Cast<Atank>(GetPawn()) mean?

It’s not a character. hierarchy is as follows

AActor
|- APawn
 |- ATank

Because the ATankPlayerController will only be used to control ATank's. From a coding stand point it could be but we know it won’t be.

Edit: on second thought I believe you’re asking why there’s no check there? Because there’s no dereferencing happening there.

i dunno what you exactly mean by this.but i checked the code again.both tank_BP and tank.c++ are a pawn.tank_BP inheriting from Tank.cpp and Tank.cpp its own is inheriting from APawn. so Cast<ATank>(GetPawn()); is a cast between two objects of the same class.although Tank.cpp is inheriting from APawn they are the same cuz we just created it as APawn class and changed its name and added some new functions.is this claim right ?

I think you’re confused as to where this code is. GetControlledTank is in ATankPlayerController not ATank or Tank_BP

i know this.i’m talking about that return type.i say that Cast is always successful because ATank class is actually the same with its parent which is APawn,just it has some more functions that we added.

Then no. It’s only what I said above.

Cast<ATank>(GetPawn());

Can fail if the pawn that is returned is not an ATank, say the ATankPlayerController is accidentally used to control an ACharacter (which is a APawn) then the cast fails. If a cast fails then it’s simply nullptr i.e. it’s equivalent to

auto Tank = Cast<ATank>(GetPawn());
if (Tank)
{
    return Tank;
}
else
{
    return nullptr;
}

i think i get it now GetPawn() returns that BP Tank which is controlled by us.and we are able to use AimAt Method if we simply return GetPawn() we cannot use AimAt. that’s why we convert it to the base class type which is always successful(cuz its an UpCast) right?

Yes to the first no to the second. It’s a downcast look at what I’ve been posting

AActor
|- APawn
 |- ATank

Now correspond the words upcast and downcast to the direction of the relationship, upcast goes up, downcast goes down.

It should always be successful for our use case because this controller is only used on tanks. If you create a character blueprint and give it this tank player controller then the cast will fail.

then what should we do if it’s a character ?
also can you say why don’t we say<ATank*> rather than <ATank> cuz GetPawn() returns a pointer.

In what situation?

That’s just how Epic decided to code it. These dynamic casts are only used with pointers so I guess they felt it was redundant.

i just didn’t get this part completely just wanted to know if there is an answer to it.so i could compare it with current code and realize it. but it doesn’t matter i will learn it later maybe.thanks really for answering me patiently.

hello DanM again.i’ve encountered another problem in the example you gave in normal c++ we were just checking to make sure we don’t encounter compiler error by using cast.can you gimme an example like the thing that’s happening in Cast(GetPAwn())? we are converting APawn* to ATank* and then we can use ATank member functions but i tried to do the same in normal c++ it seemed like it doesn’t work.

What exactly did you try?

now that i think i know the thing i was trying to do is looking stupid.to be honest i’m totally confused now.can you just show an example of it ?I wonder how a DownCast works in normal c++.after we do a DownCast, the object of base class must become completely an object of derived class,right ? i want to see that it can access derived class members.i don’t even know when it’s safe to do a downcast in normal c++. would be thankful if you can show me.thx!

That’s the thing… The example is already in this thread. My example in normal C++ is a downcast from A to B

the downcast is only when successful that we pass an object of B to the function
void test(const A *X).then there is no way that it succeed when we pass an object of A to it ?

It sounds like you want that to happen? So, why do you want that to happen?

because that was my understanding of it. i thought we can even simply do this in
int main(){} and convert an object of base class to derived and then use its members.

It doesn’t convert the underlying object. If you have an object of type A in memory you can’t turn it into an object of type B with a cast.

All the example is doing is converting the pointer to say "hey you actually point to an object of type B" The object actually stored in memory at the location in all examples in of this thread was never converted to anything.

i see thx for explanation.and for Cast<Atank>(GetPawn())the idea of working the downcast here must be like the example you gave me.but i don’t understand how are they the same.

https://godbolt.org/z/7wZYJR

1 Like

Privacy & Terms