Multiplayer: Sending Messages

Favorite This Post!
0 Votes
Posted by Lavon on May 14, 2014

Once the player is connected to a room you can start sending messages. Messages are the primary mechanism used to create your multiplayer game. Messages with a specific name and set of data, sent over by specifying a Data Container, are captured and trigger other actions in your game. For example, if you want to move a character left you can send a message called "MovePlayerLeft" and send a Data Container with "X" and "Y" properties. Then capture that event and set the "X" and "Y" property on the character spatial position property and you have a character moved by a remote player.

One thing to keep in mind while developing a multiplayer game is to try and reduce the amount of messages and data you send along because that equates to bandwidth. Your Yahoo Games Network account comes with a certain amount of bandwidth allotted each month so you will want to be careful to not go over your limit.

More on this can be found in the Yahoo Games Network docs. (https://gamesnet.yahoo.com/documentation/services/multiplayer/messages)

Setup The Game UI In GBS

%STEP_TITLE%

I created a label entity, text input entity, send button entity and received message entity. Once the UI is setup add a "Rules Component" to the send button to trigger a Multiplayer Send Message action on mouse down. Every message you send to the server will need to be sent using a Data Container or List Data component with the message properties on it. To send messages from user input copy the input text from the Text Renderer to a property on the Data Container first then send that Data Container to the server using the Multiplayer Send Message action.

Because there is no way to specify the order in which message properties are sent and received, the plugin alphabetically sorts all properties on the Data component before sending and receiving messages so that values are assigned to the correct values on either end of the message channel. If you want values to be sent in a specific order use a List Data component.

Send Message Entity Setup

%STEP_TITLE%

The logic above is added to the "MessageSendBtn" entity. There is a MouseDown condition that triggers a copy of the text the user entered to the Data Container called "MessageData". The "MessageData" component is used to transfer a message with a property called "message" to any other player in the connect room ("Lobby"). You will assign that "MessageData" component to the Multiplayer Send Message action next.

Send Message Action

%STEP_TITLE%

The Multiplayer Send Message action requires the Room ID that the player is connected to and a name for the message being sent to the server. The message name will be used to listen for the message when it broadcasted to the other players from the server.

Make sure to select a Data Container with properties from the drop-down or refer to one directly via the Property Reference field. The Data Container being sent has to EXACTLY match the Data Container used to receive a message so that the properties are transferred correctly and in order.

Use a Receive Message action somewhere in your scene to capture incoming messages from other players connected to the same room or from the server.

**Note: You can use a dynamic expression to grab the Room ID from another entity or component by clicking on the expression window icon. This could be used to connect to a dynamically created room at runtime. The MatchMaking server code that comes with this plugin sends a "MatchCreated" message from the server with a unique room id value so that the matched players can connect to a private room and play a match against each other for example.

Sending direct messages to players is built into the server code bundled with this plugin. To send a message directly to a specific player make sure to pass the player id here that is returned from the "PlayerList" message that gets broadcasts from the multiplayer server when connected to a room.

Example Data Container with "Message" Property

%STEP_TITLE%

Security Tip

It is important to try and prevent sending logic altering messages. You want to allow each client of the game to manage determining game logic and/or a winner based on the current state of the game on each client. In the Tic Tac Toe demo that there is a GameLogic entity that manages checking when the game is over based on the boards current state and how many cells are filled. The game only sends messages of the cell's state changing when it is selected.

**Note: Very important to remember that once a player submits a move using the provided TurnBased server type the server will ignore any other incoming messages from that player until the player receives the turn again. So send the "PlayerMove" event only after sending state data to the server or send the data along with the "PlayerMove" message.


Congratulations on finishing this tutorial! Found it helpful? Spread the word...


Comments

There are no comments yet!


Anyone can leave a comment!


You just need to signup for an account or log-in before you can comment.