Appearance
Changing the Player's Name
A player's name can be changed by the game client or through the LiveOps Dashboard. Either way, the name is first validated on the server against your game-specific rules.
Appearance
A player's name can be changed by the game client or through the LiveOps Dashboard. Either way, the name is first validated on the server against your game-specific rules.
ValidatePlayerName function in the PlayerRequirementsValidator class is the ultimate authority on whether a name is acceptable or not.IPlayerModelClientListenerCore.OnPlayerNameChanged is called on the game client whenever the player's name is changed.Each player has a name, and this name is stored in the PlayerModel.PlayerName field. This name is intended to allow users identify each other in multiplayer systems, for example in Guilds. Names are however not identifiers and, by default, multiple players may share a name.
When a new player is created, they are given a placeholder name, for example "Guest 1234". This document describes how to allow user pick their own name while also preventing invalid or inappropriate player names.
💡 Also on Dashboard
The validation rules also apply when changing Player's name using the LiveOps Dashboard.
Validation of names is performed by the ValidatePlayerName method in the PlayerRequirementsValidator class. This method is automatically called before updating a player's name from any source.
PlayerRequirementsValidator is a IMetaIntegrationSingleton, which means you can customize its behavior by creating your own derived class. When the game initializes, Metaplay automatically discovers and uses the most derived implementation.
The SDK provides a default implementation with basic validation (length limits, control character filtering). To customize the validation, create your own class that inherits from PlayerRequirementsValidator:
public class MyPlayerRequirementsValidator : PlayerRequirementsValidator
{
// Override length limits
public override int MinPlayerNameLength => 3;
public override int MaxPlayerNameLength => 16;
// Banned words list - consider loading from game config in production
static readonly string[] BannedWords = { "admin", "moderator", "support" };
public override bool ValidatePlayerName(string playerName)
{
// Call the base implementation for standard checks
if (!base.ValidatePlayerName(playerName))
return false;
// Disallow leading/trailing whitespace
if (playerName != playerName.Trim())
return false;
// Check for banned words (case-insensitive)
string lowerName = playerName.ToLowerInvariant();
foreach (string banned in BannedWords)
{
if (lowerName.Contains(banned))
return false;
}
// Optional: restrict to alphanumeric and spaces only
// foreach (char ch in playerName)
// {
// if (!char.IsLetterOrDigit(ch) && ch != ' ')
// return false;
// }
return true;
}
}Good to know
Whether the name is changed by the player client or from the LiveOps Dashboard, the exact same validation function is used on the Game Server.
When a player's name is changed, the OnPlayerNameChanged handler is called on the current IPlayerModelClientListenerCore. You can use this to react to name changes in your client code.
void IPlayerModelClientListenerCore.OnPlayerNameChanged(string newName)
{
// Update UI with value in newName
}To allow the player to change their own name from the client, you just need to fire off a PlayerChangeOwnNameRequest message to the server. The validation will happen on the server, and the OnPlayerNameChanged will be called if the name change succeeded.
using Metaplay.Core.Player;
...
MetaplayClient.MessageDispatcher.SendMessage(new PlayerChangeOwnNameRequest(newName));