#[Netcode] Is there a way to tell how many players (if any) are currently connected to the server?

1 messages · Page 1 of 1 (latest)

deft sleet
#

I'm running into an instance where I am sending RPCs, but there are no players connected, so I get an error. I want to prevent creating RPCs if there are 0 players currently connected.

deft sleet
#

I assume the way to do it is probably something like this:

private EntityQuery _connectionQuery;

protected override void OnStartRunning()
{
    _connectionQuery = GetEntityQuery(ComponentType.ReadOnly<NetworkStreamInGame>());
}

protected override unsafe void OnUpdate()
{

    NativeArray<Entity> activeConnections = _connectionQuery.ToEntityArray(Allocator.Temp);
    if (activeConnections.Length < 1)
    {
        //Exit early
        return;
    }
    
    //Send RPC here
}
#

(this is Entities 0.51 code, in case it looks funny)