GetController in Unreal documentation

Through this lecture Sam mentions that we know that AController :: GetController will return a APlayerController object we need of type AController

Looking at Unreals reference manual for GetControlller (it says very little as usual) but I can see that it indeed returns a pointer of AController, but how is it known that GetController will returns a pointer to APlayerController?

If Sam hadn’t said anything how would I have known?

To properly explain, would you mind giving the timestamp to the point you’re talking about?

06:34 Stephen explains why GetController can’t return a pointer of AController to pointer APlayerController.

6:45-6:59 Stephen then goes on to say that the object returned by GetController is a APlayerController. I understand what he means but was trying to work out how I could of found this out myself just referencing the documentation.

If I look at the documentation for APawn :: GetController documentation it says function returns a pointer of AController , there no mention that the AController address returned is a APlayerController.

Does Stephen mean that as there is an IS-A relationship of the APlayerController : AController that I should assume the returned AController address is also a APlayerController object?

Oh I’m dumb, I was looking at 16_ss not 16_to.

Stephen says that the function return AController* but that in this game that it’s actually pointing at a APlayerController*. It is impossible for the documentation to say what the exact type it’s pointing to.

Say you’re writing an engine and have this pawn class that stores a pointer to its controller and that controller can be one your user creates

// Engine
class APawn
{
    ??? Controller;
    ??? GetController() const { return Controller; }
};

This class needs to be able to store a pointer to AAIController, APlayerController, or any derived type a user may want to create. So what is ??? that you’re going to have in this code?

The answer is to have a base class they all inherit from. A base class pointer can point to objects of derived types. That doesn’t change the object that is in there in memory.

Demo: Compiler Explorer

However this means the return type of GetController() is AController*, it’s impossible for it to return the exact type of the controller without changing this code (and thus mean it doesn’t work with any controller).

However we know, as the developers of this game, that ATank is always controlled by the player so the returned controller is actually pointing to a APlayerController (or further derived).


Small nits:

Stephen is teaching this section not Sam.

GetController() is a function and returns a pointer, not stores one.


Hopefully the above clarifies this is not the case.

2 Likes

Yes believe I understand thanks.

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

Privacy & Terms