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

this is completely right.but how do you know about the constructor ?does unreal Controller class really have such codes in it like below:

APawn* Pawn;
public:
    APlayerController(APawn* InPawn): Pawn(InPawn) {}
    APawn* GetPawn() const { return Pawn; }

if yes then how do you know about it ? is there anywhere in the internet i can check that too ?i tried to read the documentation of it there seemed to be nothing there or mybe i wasn’t able to find it.

I don’t.

APlayerController stores a pointer to the pawn it’s controlling.
Where and when it gets that I don’t know since I’ve never bothered to look into.
However it’s irrelevent to the discussion of this question. APlayerController having an APawn* member variable is what is known and I just added a constructor for the example code to work.

1 Like

i think i get it now DanM.i checked our conversation from the start and i have come to this conclusion:in the GetPawn() method the returned type is converted to APawn*(not the essence of it completely if it’s from derived class as you said).so when we call it it to Get the pawn of our TankPlayerController it converts it to APawn* and then after that becuase we need it to be of type ATank we cast it down right ?

Incorrect. The conversion happens before that.

Like I said APlayerController has a APawn* member variable which is the pawn that it’s controlling. In essense it’s exactly like the most recent example code. Only difference is when/how it’s set.

Think about it, how would you code APlayerController to store the controlled pawn (via a pointer) to the exact type of the pawn? It’s impossible.

yes it must be like that cuz GetPawn() doens’t get any argument so it should be there already.

and this one yes because we don’t knowthe type of derived class and that’s why we generally define it as APawn right?

yes i’m just a little curious about this one. i think i shouldn’t be xD.just gonna be satisfied with the things i know now thx.

It’s defined essentially the same as what I had coded. Only difference is that it’s forced to be inline.

/** Getter for Pawn */
FORCEINLINE APawn* GetPawn() const { return Pawn; }

Well we didn’t define this, Epic did. And Epic can’t know every possible APawn class that will be created by its users.

There’s a call to SetPawn by the engine. That’s all that you need to know.

oh i think i haven’t learned much about this one.if it’s easy i’d be thankful if you could tell me if its not i will learn it later :).
and i now realize about the rest of things you explained thanks a lot!

An inline function is a function in which its body is substituted with the function call. e.g.

APawn* ControlledPawn = GetPawn();

Would end up as

APawn* ControlledPawn = Pawn;

Where Pawn is the member variable. i.e. it eliminates the overhead of the function call.


In standard C++ you would declare such function sith the inline keyword.

inline APawn* GetPawn() const { return Pawn; }

However it’s not guarenteed to actually be inlined by the compiler, it’s basically a suggestion to the compiler that it should be inlined and it’s free to decide otherwise.
These days compilers are really good at inlining so there’s really not much of a need to mark functions as inline.

With that said for a long time compilers have supported a way to force a function to be inlined.
With MSVC that’s __force_inline on GCC it’s __attribute__((always_inline))
That’s what the FORCEINLILNE macro is for so that it chooses the right one depending on which compiler is being used.

Now for why they forced it to be inline is probably to increase debug performance.
I would suggest that you just let the compiler decide for you.

i see thanks for explanations.:pray:

Privacy & Terms