C++ callback when game starts

I am trying to setup a callback that is called when the game starts. I tried several spots:

  • GameMode constructor
  • StartupModule method of the GameModule.

The code in both locations is called when the editor is started, so I guess that’s ok. However, when I hit the play button in the editor to start the actual game none of these is called.

Is there a way to receive a callback at that particular time ?

Hi,

You can override the PostLogin function in your GameMode.

/** Called after a successful login.  This is the first place it is safe to call replicated functions on the PlayerAController. */
virtual void PostLogin( APlayerController* NewPlayer );

For Example:

void AMyGameMode::PostLogin(APlayerController* NewPlayer)
{
	Super::PostLogin(NewPlayer);

	//... do something. Here, if you want, you can loop over all the player controllers to notify all player that a new player is entering...
}

Best regards

If it’s a multiplayer game, that will run every time somebody logs in and on a dedicated server possibly never.

The better way is to override HandleMatchIsWaitingToStart() in the game mode. It will be called only once when the map loads.

Hi ,

Yeah, you are right, this kind of things depend of what do you need. The HandleMatchIsWaitingToStart is another good place, but what happen if someone login with the match in progress ? I don’t know if in that case HandleMatchIsWaitingToStart is called. In this situation one option is, inside the PostLogin:

if (PC && IsMatchInProgress())
{
	//do something with this new player
} 

And yeah, its true. This PostLogin function will run every time somebody logs in. But from parameter you have the specific new player controller, so you can do whatever you want just with this player controller. And also, you have the oportunity to notify another users.

Basically its depend of your needs, probably for je42 its better to use HandleMatchIsWaitingToStart.

Cheers