DC
DC2d ago

is there a way to count the connections to the room without using the Avatar Manager

I am not using the Avatar manager at all. I would like to count the number clients connected to the room.
7 Replies
kapa.ai
kapa.ai2d ago
Hi @DC. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, there is a way to count the connections to a room without using the Avatar Manager in Normcore. You can use the Realtime component to get this information. Here's how you can do it: 1. First, you need to access the Room object from your Realtime instance. 2. The Room class has a property called clientID which represents the current client's ID. 3. You can use the datastore property of the Room to get all the client IDs connected to the room. Here's a simple example of how you might implement this:
using Normal.Realtime;
using System.Linq;

public class ConnectionCounter : MonoBehaviour
{
private Realtime _realtime;

private void Awake()
{
_realtime = GetComponent<Realtime>();
}

public int GetConnectedClientCount()
{
if (_realtime.connected)
{
// Get all client IDs from the datastore
var clientIDs = _realtime.room.datastore.GetAllClientIDs();

// Return the count of client IDs
return clientIDs.Count();
}
else
{
Debug.LogWarning("Not connected to a room");
return 0;
}
}
}
using Normal.Realtime;
using System.Linq;

public class ConnectionCounter : MonoBehaviour
{
private Realtime _realtime;

private void Awake()
{
_realtime = GetComponent<Realtime>();
}

public int GetConnectedClientCount()
{
if (_realtime.connected)
{
// Get all client IDs from the datastore
var clientIDs = _realtime.room.datastore.GetAllClientIDs();

// Return the count of client IDs
return clientIDs.Count();
}
else
{
Debug.LogWarning("Not connected to a room");
return 0;
}
}
}
This approach uses the Realtime and Room classes, which are core components of Normcore, without relying on the Avatar Manager. Remember that this method will count all connected clients, including those that might not have instantiated an avatar. If you need to track specific types of connections (e.g., only players with avatars), you might need to implement additional logic. For more information on the Realtime and Room classes, you can refer to the Normcore documentation: - Realtime - Room ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1259730396577529917> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
DC
DCOP2d ago
The GetAllClientIDs method does not exist for datastore
maxweisel
maxweisel2d ago
There is not, but you can replicate the pattern used by RealtimeAvatarManager in your own code if you'd like
Mechabit
Mechabit2d ago
AI making shit up lol
maxweisel
maxweisel2d ago
the bot has been pretty good so far but this one was pretty bad lol
liamc
liamc22h ago
I use this pattern a fair bit in all projects, not just normcore projects.
public static class InstanceRegistry<T>
{
public static IReadOnlyCollection<T> Instances => _instances;
public static int Count => _instances.Count;

public static event Action<T> OnRegistered;
public static event Action<T> OnUnregistered;

static readonly HashSet<T> _instances = new HashSet<T>();

public static bool Register(T instance)
{
if (_instances.Add(instance)) {
OnRegistered?.Invoke(instance);
return true;
}

return false;
}

public static bool Unregister(T instance)
{
if (_instances.Remove(instance)) {
OnUnregistered?.Invoke(instance);
return true;
}

return false;
}

public static bool Contains(T instance)
{
return _instances.Contains(instance);
}
}
public static class InstanceRegistry<T>
{
public static IReadOnlyCollection<T> Instances => _instances;
public static int Count => _instances.Count;

public static event Action<T> OnRegistered;
public static event Action<T> OnUnregistered;

static readonly HashSet<T> _instances = new HashSet<T>();

public static bool Register(T instance)
{
if (_instances.Add(instance)) {
OnRegistered?.Invoke(instance);
return true;
}

return false;
}

public static bool Unregister(T instance)
{
if (_instances.Remove(instance)) {
OnUnregistered?.Invoke(instance);
return true;
}

return false;
}

public static bool Contains(T instance)
{
return _instances.Contains(instance);
}
}
A basic usage could look like this:
public class ConnectedUser : MonoBehaviour
{
void Start()
{
InstanceRegistry<ConnectedUser>.Register(this);
}

void OnDestroy()
{
InstanceRegistry<ConnectedUser>.Unregister(this);
}
}
public class ConnectedUser : MonoBehaviour
{
void Start()
{
InstanceRegistry<ConnectedUser>.Register(this);
}

void OnDestroy()
{
InstanceRegistry<ConnectedUser>.Unregister(this);
}
}
But for a normcore object you could register/unregister when the realtime model is replaced, so that its initialised at the time of registration. Or if it's a poolable object you could Regsiter/Unregister in OnEnable/OnDisable, etc.

Did you find this page helpful?