Why playercontroller subclass can't use variables in parent?

I have 2 PlayerController classes: PlayerController2 extends PlayerController1.

PlayerController1 have protected variable someclass* myclass;
PlayerController1 have PostInitializeComponents() where i set myclass = spawn()…
PlayerController2 have no overriding of PostInitializeComponents(); so then PostInitializeComponents() is executed in parent class - in PlayerController1.

PlayerController2 is spawned and active. When it was spawned, then PostInitializeComponents() was executed in parent class PlayerController1 and myclass was initialized with spawned object.
But when later i try to use myclass variable in code PlayerController2 then it is NULL.

Same thing with SetupInputComponent()
SetupInputComponent() function is overrode in both classes. Also both classes are calling Super::SetupInputComponent();
But actions binded in parent class have null variables when it was called.
For example InputComponent->BindAxis(“MoveForward”, this, &ATestPlayerController1::MoveForward);
function MoveForward called every frame, but all variables there are null, even all of them was initialized in subclass.

I do not understand why, i believed that all variables initialized in parent should be valid in subclass, ans variables defined in parent class then initialized in subclass and then used in parent class should also be valid.
What is wrong with my thoughts?

Can you post the code that declares your variable in PlayerController1?

If it’s really just a C++ style pointer to ‘someclass’ then what’s happening is that the actor that you spawned gets destroyed by the garbage collector. This is because the garbage collector cannot figure out that your class is holding a reference/pointer to that actor. It can only do this for variables that are marked up with the UPROPERTY() macro, because then special magic sauce code will be generated to ensure that the garbage collector can see that variable. Therefore, the solution would be to declare your variable as follows:



UPROPERTY()
someclass* myclass;


Dear Gerke,

Some day I am going to quote you in of my wiki tutorials on exactly this point, and refer people to SpecialMagicSauce.h for more information

:wink:

Rama