I am working on the logic for an elevator, and I want the player to press the ‘E’ key after entering a specific area, which will make the elevator move up or down. To achieve this, I want to call the elevator class logic from the PlayerController. However, it seems that the PlayerController is created before the elevator, and I cannot access the elevator instance in the game. Is there a way to solve this?
Hi
In order to get your PlayerController
you can use the Pawn Template function GetController
or simple use UGameplayStatics
.
In your pawn derived class:
APlayerController* PC = GetController<APlayerController>();
Using your pawn’s reference:
APlayerController* PC = MyPawnRef->GetController<APlayerController>();
Using UGameplayStatics:
APlayerController* PC = UGameplayStatics::GetPlayerController(this, 0);
bye.
No, I mean that the player controller cannot access the instances of other classes during the game, and it cannot get the elevator instance at the start of the game.
Line trace from the player controller towards the center of the screen and get a hit result actor.
Then call an interface on the hit actor.
Have the elevator implement the interface and have functions to react to the interaction.
No need to keep a reference of the elevator inside of the controller.
In this case the interaction with the elevator can show a ui with floors to pick.
Or you can have a 3d widget in worldspace that can have buttons to change floors.