Where is the right place to bind GetWorld()->GameStateSetEvent?

I want to bind a function to this event. (GetWorld()->GameStateSetEvent)

I want to do it in the controller and I also want to do it in the widgets.
However I can’t find the correct event to bind it.
Sometimes it works and sometimes it fails.
So knowing the right place where it never fails would be great.

Thank you so much!!


UPDATE:


This does not work because sometimes the widget is created after the GameState… so it never know it happened.

And in the player controller maybe this could be a good place but i’m not complely sure…

void AMyPlayerController::ReceivedPlayer()
{
	Super::ReceivedPlayer();

	UWorld* World = GetWorld();
	if (!IsValid(World))return;			
		
	World->GameStateSetEvent.AddUObject(this, 
    &AMyPlayerController::HandleGameStateSet);		

}

So… any ideas are wellcome!!

What you can do is register the event and then immediately call the function as well to handle the initial update.

World->GameStateSetEvent.AddUObject(this, &AMyPlayerController::HandleGameStateSet);
HandleGameStateSet(World->GetGameState());

Of course the handler needs to deal with the case that the gamestate is not yet valid

1 Like

It’s a very good idea. I’ll do it like that.

What do you think about ReceivedPlayer()… Is it a good place to do it?
Or do you think there is a better place?

Thank you very much for your help!!

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