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
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:
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:
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:
To add a new player:
#### 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:
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:
----------------------<@446432643115122688> Kapa.ai is still learning and improving, please let me know how I did by reacting below.