How to get a reference to player character in C++ after all the actors have been initialized in the world?

Hello! I am very much having a hard time getting the reference to the player character because when my system manager is initialized, the player is not yet spawned in the world.

So I asked a friend of mine for help and he directed me to use FWorldDelegates::OnWorldInitializedActors which is a delegate that gives a callback when all the actors in the world are initialized. However, I am not sure how to subscribe to this delegate, specifically I want to make a function that will get the player character’s reference after the notification.

Currently, the code gives me compiler errors (linking errors). The following code below is what I currently have right now:

void URU_UISystemManager::Initialize()
{
	UE_LOG(LogTemp, Warning, TEXT("I am Initialized!"));
	
	FWorldDelegates::OnWorldInitializedActors.AddUObject(this, &URU_UISystemManager::HandleWorldInitializedActors);
	//UE_LOG(LogTemp, Warning, TEXT("Pawn's Name is: %s"), *MainPlayerCharacter->GetName());
	
}

void HandleWorldInitializedActors(const UWorld::FActorsInitializedParams& someActorsInitializedParams)
{
	APawn* MainPlayerCharacter = GetWorld()->GetFirstPlayerController()->GetPawn();
	UE_LOG(LogTemp, Warning, TEXT("Pawn's Name is: %s"), *MainPlayerCharacter->GetName());
	
}

Any advice or help is appreciated

My advice would be to check for the players character when you need to access it, rather than trying to find it at a specific point. The default GameMode, last I knew, doesn’t spawn a character until the player clicks to spawn … and if the character dies, then they can respawn a new character… as well, if you’re doing multiplayer, there could be multiple playercontrollers and characters…

so it’s probably in general a bad architecture to try to cache a player character reference. of course, i don’t know the rules of your game. If you can only ever have one player character, and it’s spawned at start in your gamemode, then you can just notify this manager thing from gamemode.

You could have your player controller register with the UI System Manger on BeginPlay().

Add a check if its already registered and you are GTG.

The idea is to get a reference once in order to get the health component I made inside of it. And then i have a damage function that is called whenever it gets hit. I have a dispatcher that fires when it gets hit. The only thing that I want is to get its values at the start and continuously listening for update (health - 1) and then update my UI (the system i’m trying to do)