Multiplayer tutorial 3: pong

18

Index

Features on these Courses

Stats

31,071 visits, 109,289 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 22 Apr, 2014. Last updated 11 Jan, 2022

Game events

As with the chat example we have global constants for the game, instance and room name. These allow us to conveniently change the names on the signalling server without having to hunt through our events. However we also have a new global for the game state. The first player joining a room needs to wait until someone to play with also joins. Then the game will say "get ready", then "GO!". This global variable holds the current state.

Message log group

The previous chat example used a Text Box object set to textarea mode as the main chat and information log. However it's an opaque object so would obscure the layout if used in this example. Instead we use an ordinary Text object called LogText to show log messages. However normal Text objects don't support scrolling. To make sure messages keep being displayed even as they go off the bottom of the object, we implement our own scrolling system. This is done by using an array to store a line of text in each array index, and then deleting the first item to scroll it up a line. This is not specifically related to multiplayer games, but is a useful technique to cover anyway.

The AddLog function adds a new line by adding it to the end of the array ("push back"). If there are over 20 lines, it deletes the first line ("pop front") - so the first line will disappear from view. After that we completely reset the content of the LogText object: first it's cleared to empty, then we iterate the array and add each line in to the object in order. The end result is each time we call AddLog a new line is added to the end of the LogText object, and it scrolls up when it reaches 20 lines.

Now we can call AddLog in the Function object from anywhere else in the event sheet to conveniently add scrolled messages to LogText.

  • 4 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Re: "Sync object will automatically create and destroy objects, but when they are created we need to set their peerid instance variable so we can later know which peer the object represents."

    => Why don't we use the Sync instance variable action in order to complete the Paddle object synchronization with its peerid instance variable value?

  • does this tutorial still work? if i am a host i don't see the peer movement

  • как сделать систему очков?хочу чтобы счет у двоих показывало