#As a non-host client, how do I get a list of joined players?

13 messages · Page 1 of 1 (latest)

cerulean pine
#

I learned about this:
foreach (NetworkConnection item in InstanceFinder.ServerManager.Clients.Values)
But that's on server side only.
I tried:
foreach (NetworkConnection item in InstanceFinder.ClientManager.Clients.Values)
Doesn't work for non-host client..

foreach (NetworkConnection item in InstanceFinder.ClientManager.Clients.Values)
{
    // item.Objects.Count returns 0
    foreach (NetworkObject nob in item.Objects)
    {
    }
}```

So... what is the proper way for non-host client to get a list of joined players?
wide shoal
# cerulean pine I learned about this: `foreach (NetworkConnection item in InstanceFinder.ServerM...

hello Irish,

so I'm still a newbie with FN but I have been using it for last few weeks and slowly getting hold of it, if anyone reading this find any thing wrong with my understanding of the subject, please feel free to correct.

with that out of the way, here's how I'm doing it, working fine for me so sharing it here hoping that it will work for you as well.

  1. you can implement a unique id (could be virtually anything) for your players and make it a [SyncVar] that should be updated on the server using a [ServerRPC] when this player is created.

  2. then, in your SyncVar's OnChange method, store this player in a dictionary locally with this unique id for your player being the key.

  3. and you shall have a locally accessible players list

Explanation:
SyncVar is updated on the server and synced with the clients via OnChange method that is called on each client when Server updates a SyncVar, so because it is called on each client instance, you can easily update your players list in your OnChange method.

untold bay
#

@cerulean pine@wide shoal
This is actually built-in already 🙂

#

On the ServerManager component is ShareIds, which should be enabled by default. When enabled even non-host clients are aware of all players within ClientManager.Clients

cerulean pine
#

Wait... you said all players will have this information in ClientManager.Clients... but ....

// InstanceFinder.ClientManager.Clients.Values.Count does return 2 ... but...
foreach (NetworkConnection item in InstanceFinder.ClientManager.Clients.Values)
{
    // item.Objects.Count returns 0 <--- This is where I have a problem with. Why does this return zero?
    foreach (NetworkObject nob in item.Objects)
    {
    }
}```
#

See the question I have in the comments above...

wide shoal
royal lanceBOT
#

FirstGearGames received thanks.

cerulean pine
#

Um, okay but what about the item.Objects returning nothing? Hmm. That's where I'm stuck on.

untold bay
#

No need for it to

#

it doesnt let you see everyones objects

wicked relic
#

There's a case to be made about game styles where you don't want players to know how many people are on the server. Tarkov, Albion, etc. But those are definitely edge cases 😄