xulipa
xulipa•3mo ago

Store list of player states

I want to check the readiness of players, to identify who is currently playing which means the rest of the players cannot currently play. So I'm thinking I need to retrieve client IDs locally and then add that to a dictionary or a realtime set?
Solution:
you can just add a model to the avatar which syncs a bool for ready
Jump to solution
10 Replies
kapa.ai
kapa.ai•3mo ago
Hi @xulipa. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you're on the right track. You can use Normcore's RealtimeAvatarManager to keep track of all avatars in the room. The avatars property is a dictionary of all RealtimeAvatar components stored under the clientID that the avatar belongs to. This can be useful for determining how many players are currently in a multiplayer space. Here's an example of how you can use RealtimeAvatarManager:
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;

private void Awake() {
_manager = GetComponent<RealtimeAvatarManager>();
_manager.avatarCreated += AvatarCreated;
_manager.avatarDestroyed += AvatarDestroyed;
}

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar created!
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
using UnityEngine;
using Normal.Realtime;

public class MyAvatarManager : MonoBehaviour {
private RealtimeAvatarManager _manager;

private void Awake() {
_manager = GetComponent<RealtimeAvatarManager>();
_manager.avatarCreated += AvatarCreated;
_manager.avatarDestroyed += AvatarDestroyed;
}

private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar created!
}

private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {
// Avatar destroyed!
}
}
In this example, AvatarCreated and AvatarDestroyed are methods that you can use to update your dictionary or realtime set when a player joins or leaves the game. For storing player states like readiness, you can use a RealtimeDictionary or a StringKeyDictionary. These collections let Normcore know when their contents have changed, allowing it to detect and sync their contents automatically. You can store collections of RealtimeModel objects in these collections. Remember, only the local player can update values in realtime components on their own avatars. So, you should store data like readiness in a RealtimeDictionary or a StringKeyDictionary instead of trying to modify the avatar directly. For more information, you can refer to the following documentation: - RealtimeAvatarManager (https://normcore.io/documentation/realtime/xr/avatars#realtimeavatarmanager) - RealtimeDictionary and StringKeyDictionary (https://normcore.io/documentation/room/common-questions#why-cant-i-just-sync-a-dictionary-or-list) - Storing player states (https://normcore.io/documentation/realtime/common-questions#im-trying-to-store-my-players-health-in-a-realtimecomponent-on-my-avatar-how-can-i-let-other-players-modify-this-values-or-others) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai•3mo ago
<@725231244169707600> kapa.ai is still learning and improving, please let us know how it did by reacting below
Solution
Mechabit
Mechabit•3mo ago
you can just add a model to the avatar which syncs a bool for ready
Mechabit
Mechabit•3mo ago
or if you want to use a dictionary here's an example https://gist.github.com/d12/f577bb50b0fdeeb17084819c78fe5c43
Gist
How to use RealtimeDictionary
How to use RealtimeDictionary. GitHub Gist: instantly share code, notes, and snippets.
Mechabit
Mechabit•3mo ago
swap score for ready state
xulipa
xulipa•3mo ago
Okay thank you Mechabit yeah I assumed I need to use a RealtimeDictionary or a RealtimeSet. It's also about time I dive into this 🙂
Mechabit
Mechabit•3mo ago
ok but one thing about collections, you wont get events for models changed within the set so if someone readys and then unreadys you'll only get the event for model added unless you specifically register for each model's update event
xulipa
xulipa•3mo ago
Ohh I see. So that wouldn't be optimal. Then RealtimeDictionary definitely seems to be the way to go.
Mechabit
Mechabit•3mo ago
one thing you can do is have an int model that you increment each time some change has happened so when someone readys or unreadys they also increment the int so the ready state model will have 1 property for event trigger and 1 property which is a collection of player states
xulipa
xulipa•3mo ago
Hell yeah that's also how I have done the ReadySyncModel now it just increments by 1 and then to 2 when 2 players are ready. Then I just need to add that to the RealtimeDictionary as a Key Value pair with the Client ID 🙂