Multiplayer communication is handled via the Player.IO service by connecting players to rooms. All players in a room can send messages to each other. If you want private communication between certain players connect the players to a separate room that only those players know about. You can also send messages directly to another player by passing a specific player username in the send message action. A list of all connected players is returned with the "PlayerList" event if you use any of the server types that come with this plugin.
**Note: Room connections can only be made once a player has connected to the server either by registration or login.
Player.IO Room Connection
Here we just connect the current logged in player to a default room called "Lobby".
The Room ID is a unique name of a room created on the server. If the provided Room ID has not been created on the server yet this action will create a new room with that id and connect the current player to the room.
The Room Type is the name of the game server type that we uploaded earlier in this guide. You can have many different room types that run different game functions on the server. We have provided a number of them for you to take care of matching players, and enforcing turns in a turn based game like tic tac toe. If you want to add custom functionality write custom game server code and pass the room type name here. Player.IO provides a lot of documentation for this if you are a programmer (https://gamesnet.yahoo.com/documentation/services/multiplayer/serverside).
The Private Room checkbox will exclude the created room from being returned in any list room action results.
Additional information about the newly created room and player can be passed along to the server via the Room Data and Player Data options. You can use a Data Container or List Data Container component to send properties to the server. Once a Data Container is created on the entity it will appear in the drop-down of this action or reference one directly by clicking the Property Reference icon in the input field. The properties passed along to the server can be accessed from the game server code to perform custom logic but this is not necessary for this demo. That is a more advanced topic.
Once a successful connection has been created to a room you can take the Room ID and start sending messages to all connected players within that room via the Multiplayer Send Message action.
There is a Multiplayer List Room action that will allow you to query the server for all available public rooms for your game. This could be a way to list currently active matches available for a player to connect to. Pass a Room Type name and a List Data container to store the RoomInfo objects to. You can then loop through the results once they have been returned. The object data that is returned for each room will consist of the following properties.
Room Info object:
- data - the object with all the room properties that were passed when the room was created with a Multiplayer Room Connect action.
- id - the unique id/name of the room. Can be used to connect the player to this room.
- onlineUsers - the number of users currently connected to the room.
- roomType - the name of the server type
Sample Connection Flow Diagram
Sending Extra Player Data
The Player Data drop down is used to send additional data about the player that is trying to connect to the room. This could be used to pass along data for matching two players with similar attributes to the same room for example. If you are a programmer you can access this extra player data on the server side to make decisions. We have provided a Match Making server for you with this plugin that will read the player values passed to it and send back a room id for 2 or more players to connect to.
The "key" value is the name of the property on the Data Container. Only String values are transferred to the server via GBS.