Multiplayer tutorial 3: pong

18

Index

Features on these Courses

Stats

31,697 visits, 110,571 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

Host group

The host is responsible for actually running the game. Sync Object does most of the work keeping the peers up to date. The host handles actual game logic, so the Host events are a little more involved than for peers.

First of all, when we are the host we're only waiting for one other player to join. Therefore the first time On peer connected triggers, we can begin the game. When a peer connects we first create a paddle for them on the right of the screen and associate it with the joining peer. The game should say "Get ready...", wait 3 seconds, say "Go!" for 1 second, then switch to displaying the score (which is the "started" game state). When the game starts we also enable the ball's bullet movement and set it off at a random angle. Remember only the host actually moves the object; Sync object tracks it and updates it for the peers, and the behavior should only be enabled on the host, since otherwise it could conflict with Sync object on the peer.

The host uses a function to set the game state. This is because simply changing the GameState variable only affects the host, and the peer would never find out. This function allows us to both change the varible, and send a message to the peer indicating the game state has changed. The peer receives this and updates its own GameState variable. This keeps the variable updated for everyone in the game.

Each paddle's peerid variable is set to the ID of the corresponding player, and we can use this to pick the object for individual players. Since this is a two-player game, the peer ID is either MyID and is our own paddle, or it is different and it's the other player's paddle. We need to move the other player's paddle to where they have it, so we pick their paddle and set it to their "y" input value. The latest input value for a peer is retrieved with the PeerState expression.

Only the host handles gameplay logic like handling collisions. In this case the host is responsible for bouncing the ball off the paddles; this happens automatically because the paddles are solid and the Bullet behavior is set to bounce off solids (remember the peer has the Bullet behavior disabled so this still only happens on the host). Each time it bounces we also add to its speed so the game gets harder and harder.

Events 29 and 30 track the score. If the ball goes off the left of the screen (X < 0), add 1 to the peer's score (the player on the right), and reset the ball. If the ball goes off the right of the screen (X > LayoutWidth), add 1 to the host's score (the player on the left), and reset the ball.

That's all for the host-specific events.

  • 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

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