Event timing problem with remote player state

Hi, I seem to have a problem with properly timing events when trying to refresh player UI in a network game. I need to present some initial data on the UI, which is a widget component in my pawn. This implies I need to set it after BeginPlay has been called, otherwise the widget does not exist yet. The data is owned by PlayerState.

On the server, I wait for PawnSet to fire, then I call a replicated event on the Pawn owning client with the needed data, so it can be presented on the UI in the client. The problem is that PawnSet and the resulting replicated event is fired before BeginPlay is called on the Pawn, so the UI doesn’t exist yet. Is there a way to make sure I can call replicated events on the owning client after the Pawn has been fully initialized (BeginPlayer has finished)?

Yes. What you should do instead is set the UI directly in the pawn class and pull the data from PlayerState when it has replicated to clients. That’s why you got APawn::OnRep_PlayerState() which fires when the PlayerState reference inside the pawn class replicates. There you can pull the data you want from the PlayerState and update your UI as you are on client.

By the way, if you look into that function you will see that it calls APawn::SetPlayerState() which futher down broadcasts the delegate you mentioned OnPawnSet. Though, that’s not the only place it’s broadcasted from. For example, it’s also broadcasted when the pawn is possessed (see APawn::PossessedBy()), which happens early on, on the server, causing you problems.

1 Like