Why Cast?

Why are we casting to the Character instead of just doing AShooterCharacter* Character = OwnerComp.GetAIOwner()->GetPawn();

Correct me if I were wrong, my understanding is that GetPawn() gives you a pawn pointer, which is not AShooterCharacter pointer, so if you just assign Pawn* to AshooterCharacter* with mismatched type, it won’t work.

So Sam here uses a dynamic cast here to cast the Pawn class to ShooterClass making sure this command here is type safe.

I’m not sure I 100% digested this neither, if my previous comments are correct about casting, it seems we are sort of converting a class into another then use a method from the desired class to do something. Is there a way that we can directly reference to the AShooterCharacter class?

1 Like

It’s needed in order to call Shoot. You wouldn’t be able to call it without casting as APawn doesn’t have that function.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms