Skip to main content

Player Management

Handling players -- connecting, spawning, identifying, and occasionally removing them -- is a core part of any multiplayer world. Here's how HELIX gives you control over the player lifecycle.

Player Connections

When a player connects to your world, the server fires an event you can hook into. This is where you set up their character, load their data, or send them a welcome message.

// Override in your GameMode
void AMyGameMode::OnPostLogin(AController* NewPlayer)
{
Super::OnPostLogin(NewPlayer);

// Spawn a character for the new player
FVector SpawnLocation = GetRandomSpawnPoint();
AMyCharacter* NewCharacter = GetWorld()->SpawnActor<AMyCharacter>(
CharacterClass, SpawnLocation
);
NewPlayer->Possess(NewCharacter);

UE_LOG(LogTemp, Log, TEXT("Player connected: %s"), *NewPlayer->GetName());
}

Getting Player Info

Every connected player has a unique ID, a display name, and various properties you can query.

// Get player details from a PlayerController
FString PlayerName = PlayerController->GetPlayerName();
FString PlayerID = PlayerController->GetUniqueNetId();
int32 Ping = PlayerController->GetPing();

Iterating Over Players

Need to loop through everyone online? Maybe for a scoreboard, or to apply an effect to all players:

// Loop through all connected players
for (auto It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
{
APlayerController* PC = It->Get();
// Do something with each player
}

Kicking Players

Sometimes you need to remove someone -- whether it's for bad behavior or because the round ended.

// Kick a player with a reason
AGameModeBase* GM = GetWorld()->GetAuthGameMode();
GM->GameSession->KickPlayer(PlayerController, FText::FromString("Kicked by admin"));

Disconnect Handling

Don't forget to clean up when players leave. Destroy their character, save their data, and free up resources.

void AMyGameMode::Logout(AController* Exiting)
{
// Save player data, clean up their character
SavePlayerData(Exiting);
Super::Logout(Exiting);
}

Always handle disconnects gracefully -- players can drop unexpectedly due to network issues, not only from intentional logouts.