Appearance
Appearance
ValidatePlayerName
function in the Game Server 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.Validation of names is performed by a single function, called ValidatePlayerName
, in PlayerActorBase.cs
and it is automatically run before updating a player's name from any source.
The Metaplay SDK contains a reference implementation that looks like this:
// Snippet from PlayerActorBase.cs
public static bool ValidatePlayerName(string name)
{
// Validate that length of name is between 5 and 20 characters long.
if (name.Length < 5 || name.Length > 20)
return false;
// Check that no control characters are inlcuded.
foreach (char ch in name)
if (Char.IsControl(ch))
return false;
// Filter out semi-colons to mitigate any attempts at sql-injection attacks.
if (name.Contains(';'))
return false;
<!-- markdownlint-disable-next-line MPL006 -->
// [todo] Add more validation steps here as necessary, each one
// returning false if the name fails to pass.
// All steps passed - The name is valid.
return true;
}
Fun fact: the above implementation allows emojis in names, so in real life, you might want to do something more restricted depending on your game's character support πΊπ¦.
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));