-
-
Notifications
You must be signed in to change notification settings - Fork 305
Thread Safe Player Iteration
Sometimes it is important for our server to go through every player on the network and perform some action for them. Being that the players can connect asynchronously to the server, the Players list can be altered at any point, even while we are currently iterating through the players. There are two ways to be able to go through the players in a thread-safe manner WHICH YOU MUST ALWAYS DO. You either will be required to lock the players before performing any actions on them, or you can use the provided helper method NetWorker::IteratePlayers.
If you were to use a simple lock, then let's say we had a reference to a NetWorker with the variable name myNetWorker
. Now you have direct access to the players via the myNetWorker.Players
getter, however it is not thread safe to just simply access them in this way; you instead will lock them as seen in the following:
lock (myNetWorker.Players)
{
// Do your player iteration logic here
}
The second way would be to use the NetWorker::IteratePlayers method as described above. There are two ways that you can do this, the first being to provide a lambda expression for quick inline actions and the other is to provide a function pointer.
// This is an example of using a lambda expression
myNetWorker.IteratePlayers((player) =>
{
// Do your player iteration logic here
});
// This is an example of using a function pointer
myNetWorker.IteratePlayers(GoThroughPlayers);
// ...
private void GoThroughPlayers(NetworkingPlayer player)
{
// Do your player iteration logic here
}
Getting Started
Network Contract Wizard (NCW)
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
Master Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
-
Connection Cycle Events
-
Rewinding
-
Network Logging
-
Working with Multiple Sockets
-
Modify Master and Standalone servers
-
NAT Hole Punching
-
UDP LAN Discovery
-
Offline Mode
-
Ping Pong
-
Lobby System
-
Upgrading Forge Remastered to Develop branch or different version
-
Forge Networking Classic to Remastered Migration Guide
-
Script to easily use Forge Networking from sources
-
Run Two Unity Instances with Shared Assets for Easiest Dedicated Client Workflow