Appearance
Appearance
The way the SDK stores data on the server is by using Models that contain data for entities shared by the client and the server. In this page, we'll focus on the Player Model, which represents the state of a player.
You can use the different Models to store everything needed to reconstruct a player's game state, such as stats, level, inventory, and even their whole base in a city-building or base-building game. The Player Model specifically functions as the in-game equivalent of a game account. A PlayerModel.cs
file was created for you inside the Assets/SharedCode
folder of your Unity project when you initialized the Metaplay SDK into it, feel free to extend it and add more properties and methods!
Here's what an example Player Model could look like:
// The PlayerModel class should contain all the state of the player.
[MetaSerializableDerived(1)] // Mark the class as serializable
public class PlayerModel : PlayerModelBase<...>
{
/// Game specific examples of properties
[MetaMember(209)] public PlayerStats Stats { get; private set; }
[MetaMember(210)] public PlayerInventory Inventory { get; private set; }
public const int TicksPerSecond = 10;
protected override int GetTicksPerSecond() => TicksPerSecond;
/// Execute a single tick on the PlayerModel.
/// By default this is called 10 times per second,
/// you can change TicksPerSecond to change the behaviour.
protected override void GameTick(IChecksumContext checksumCtx)
{
}
}
The [MetaMember(tagId: 123)]
attribute marks members that will be persisted on the database. The Player Model itself is automatically persisted. Each TagId
has to be unique and we recommend that you do not re-use TagId
s. For more info about serialization, see Deep Dive: Data Serialization.
Using MetaplayClient.PlayerModel
you can read from the Player Model anywhere from client, and you can write to the model using actions, which we'll be covering in the next guide.
Check it out!
The Metaplay SDK ships with a Model Inspector that displays your game's Models properties in real-time while in Play Mode. Take a look at Model Inspector to learn how to use it.
You can update Models over time by attaching game logic to Ticks, as in the above PlayerModel
sample code. For example, you could use this to simulate timers or other game time-dependent logic while ensuring the client and server Models stay in sync.
For a deeper exploration of how the execution timeline of Ticks and Actions works, have a look at the Deep Dive: Game Logic Execution Model page.
Now that we can save data, we need to be able to modify it. The next guide, Creating and Using Actions details how you can use actions to modify and update data in your Models, be it the Player Model or otherwise.
There were some other pages mentioned in this guide that you might find interesting:
[MetaSerializable]
attribute works.