Hey so in the Tick function I checked whether player controller was true and it always returned true but when the OnGameEnd() function was called I checked player controller and it is null?? But then the same frame in the Tick function it returns true? I really need to use the player controller in my OnGameEnd function so I’m stuck…
Can someone please help me, any help is much appreciated thank you!
Depends on when and where you are trying to access the PlayerController from. I don’t know when is your OnGameEnd() function is called or what it is tied to (a delegate, or just called from game mode?) but there is a function that you can override in controllers.
// .h
virtual void GameHasEnded(class AActor* EndGameFocus = nullptr, bool bIsWinner = false) override;
// .cpp
AMyPlayerController::GameHasEnded(AActor* EndGameFocus, bool bIsWinner)
{
Super::GameHasEnded(EndGameFocus, bIsWinner);
// your code here
};
Thank you for the code above. I am doing something similar.
No, the function is just called from some random actor and I created another random Test() function and called it again from the random actor and still player controller is null and I can’t any bools variables, this is so odd
Edit: Now whenever I call if (Player->GetPlayerController()) {} the game crashes?
That’s because the player has already been destroyed, so you’re accessing a null pointer, which causes a crash.
Whatever you need to do with the player controller, do it before the game actually “ends” (i.e. before things start getting destroyed). For example, if the player presses ESC to close the game, before calling QuitGame, you would save the current progress.
No because in the Tick function I’m checking whether player controller is true then printing a message whether it is true or not and it is always true, even after that function call but just in that frame for some reason, player + player controller = null and I still can’t set the value of any bools?
Also I’ve removed all game ending code so its just an empty function with print logs…
It might be something to do with the actor I’m calling the function from which is some random actor?
Update: Just tried to use a delegate instead of a direct call, and everything isn’t null now, so that is my temporary fix, do you have any idea why though?
The only reason I can think of is the delegate accesses the player controller before it’s null, but idk if that’s the reason.
Another thing you can do is, since you know you can access the player controller in the tick function, just store a reference to it in a variable during the tick (or BeginPlay), and then use that when the game ends.
If the thing you need from the player controller isn’t a function or a variable that will change, you can just store the variable instead of the player controller.