rj
rj15h ago

Player Health mechanic

In the docs its mentioned to use RealtimeDictionary for something like this, but I'm having trouble getting a class set up with this.
2 Replies
kapa.ai
kapa.ai15h ago
Hi @rj. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To implement a player health mechanic using Normcore, the recommended approach is to use a RealtimeDictionary to store each player's health as a model. This allows multiple clients to modify health values without causing datastore conflicts, and all updates are synchronized efficiently across clients. How to Set Up a Player Health Mechanic with RealtimeDictionary #### 1. Create a Health Model First, define a model to represent the player's health. For example:
using Normal.Realtime;
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class PlayerHealthModel {
[RealtimeProperty(1, true, true)]
private int _health;
}
using Normal.Realtime;
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class PlayerHealthModel {
[RealtimeProperty(1, true, true)]
private int _health;
}
This model will store the health value for each player. The [RealtimeProperty] attribute ensures the health value is synchronized and fires change events when updated. #### 2. Add a RealtimeDictionary to Store Player Health In your main game model, add a RealtimeDictionary to store each player's health model, keyed by their client ID or another unique identifier:
using Normal.Realtime;
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class GameStateModel {
private RealtimeDictionary<PlayerHealthModel> _playerHealth;
}
using Normal.Realtime;
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class GameStateModel {
private RealtimeDictionary<PlayerHealthModel> _playerHealth;
}
You can then add, update, or remove health models for each player as needed. #### 3. Access and Modify Health To update a player's health, retrieve their PlayerHealthModel from the dictionary and set the health value:
uint playerId = ...; // The player's unique ID
PlayerHealthModel healthModel;
if (model._playerHealth.TryGetValue(playerId, out healthModel)) {
healthModel.health = newHealthValue;
}
uint playerId = ...; // The player's unique ID
PlayerHealthModel healthModel;
if (model._playerHealth.TryGetValue(playerId, out healthModel)) {
healthModel.health = newHealthValue;
}
To add a new player:
model._playerHealth.Add(playerId, new PlayerHealthModel());
model._playerHealth.Add(playerId, new PlayerHealthModel());
#### 4. Listen for Changes You can subscribe to the modelAdded, modelRemoved, and modelReplaced events on the RealtimeDictionary to react to changes in player health models:
model._playerHealth.modelAdded += (dict, key, healthModel, isRemote) => {
// Handle new player health model
};
model._playerHealth.modelAdded += (dict, key, healthModel, isRemote) => {
// Handle new player health model
};
Important Notes - RealtimeDictionary is designed for real-time updates and can be modified by multiple clients simultaneously. All updates are applied instantly and rolled back if rejected by the server due to ownership issues. - Only supported primitives (like int, float, etc.) can be serialized in models. If you need to sync more complex data, break it down into supported fieldsCommon Questions. - Do not use standard C# collections like Dictionary<> or List<> for synchronization, as Normcore cannot track their changes automatically. Use RealtimeDictionary, RealtimeSet, or other provided collections insteadCollections. For more details, see the RealtimeDictionary reference and the Common Questions section in the docs. If you need a concrete example or run into a specific error, please provide your code snippet or error message for more targeted help! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai15h ago
<@446432643115122688> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?