How to access all client connections from the server
During a game, we often need to work with all the connected clients at once, such as sending out a game update. In this recipe, we will learn how to go through the list of all the connected clients and do something with them.
How to do it...
The ClientGroup
collection is a SimGroup
collection that stores each of the client's GameConnection
instances on the server. The easiest method of stepping through this group and working with each connection instance is through the TorqueScript foreach()
operator:
// Step through each client in the ClientGroup on the server foreach(%client in ClientGroup) { // Play a thunder clap sound on the client. The play2D() // method sends a network sound event to the client // for the sound to be played locally. %client.play2D(ThunderCrash1Sound); }
How it works...
The preceding code uses the
foreach()
function to retrieve each client in the ClientGroup
list, and does some work on it. In this example,...