Getting started with peer to peer, turn based multiplayer games.

5

Attached Files

The following files have been attached to this tutorial:

.capx

peer2peerexample.capx

Download now 177.61 KB

Stats

6,173 visits, 8,470 views

Tools

Translations

This tutorial hasn't been translated.

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 11 Apr, 2015. Last updated 19 Feb, 2019

The possibility of adding a multiplayer component to your game is an exciting prospect indeed!

Here are some simple techniques that should help a beginner or novice get started with a peer-to-peer, turn based multiplayer game. By peer-to-peer I mean 2 computers connecting directly to each other without the need of a server handling the game logic. Specifically, this tutorial will cover how to effectively send messages between 2 computers using the multiplayer object.

First up: Understanding the differences between host and peer.

When 2 peers connect to each other, one becomes the host and the other becomes the peer.... Waittt just a minute here, I thought we were talking about PEER TO PEER games, not host to peer. Well, we are and this is the first lesson.

In a peer to peer game, whoever joins the game first will automatically become the "host", but in our game the host has no special privileges and not much that separates it from the second player who joins, the "peer".

Here is the important part: The only difference we need to make note of between the "host" and the "peer" for our setup is how they send messages to each other.

Hosts BROADCAST messages to Peers.

While Peers SEND messages to the host.

Once either message has been sent, we intercept it using the "on peer message received" condition combined with the tag line attached to that message.

Making the most of Multiplayer.Messages.

So at this point, it is clear that we will be using messages to interact with other peers. So how do we make the most out of those messages? Or more specifically, how do we reduce the amount of messages that have to be sent in order to keep the game updated on both clients?

The answer is the "tokenat" expression.

The tokenat expression allows us to combine unrelated bits of information into a single message, later to be broken apart again by the client receiving it (see Cap.x for example).

What to do with the messages received/How we move the game forward.

This will likely be the trickiest part for most beginners to wrap their head around. Take a break for a moment and imagine you are playing chess with a person with no arms. If he or she cant physically move the pieces then the only way you could play is if you were given instructions on where they wanted to go, what they wanted to do. That way you could move the pieces for them.

And this is exactly how we build the game. When either client/peer receives a multiplayer.message, we have functions that decode that message and keep the game moving forward.

See the Cap.x for an example of a very simple board game where you simply take turns moving your piece around.

I hope this has provided a base for you to build your own peer to peer multiplayer game. I look forward to seeing all of your creations! :)

I have also recently added a trading card game template to the store, so for anyone planning on making a card game, if you are still struggling or would like a head start that option is available.

-

Online Trading card Game

.CAPX

peer2peerexample.capx

Download now 177.61 KB
  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!