Multiplayer tutorial 4: real-time game

Favourite 76 favourites
Tutorial written by AshleyOriginally published on 19th, March 2014 - 4 revisions

Common group

The Common group has events that are used for both the host and peers.

When peers connect and disconnect, we add a log just like in the chat tutorial.

https://www.scirra.com/images/articles/mp3-ev68.png


Our own peer is also updated so its lookatx and lookaty variables are set to the mouse position. For peers, these values are sent as client input values to the host; on the host we update it anyway so we can treat all players consistently.

https://www.scirra.com/images/articles/mp3-ev70.png


Every tick, we need to update a few things. First of all every peer is made to point towards its look position, and their associated AimSpot placed at the same position. The PeerName labels are positioned above each peer with some stats including the player name, health, kills and deaths. Finally, we always move objects to the appropriate layer. The reason for this is Sync object does not transmit which layer an object is on to save bandwidth, since for most games objects remain on the same layers. As a result Sync object might create objects on the wrong layer for peers, so we make sure everything is moved to the correct layer.

https://www.scirra.com/images/articles/mp3-ev71.png


We also display which peers are firing. This event is purely cosmetic: the actual hit testing happens only on the host, and has already been covered. However it would look strange if damage was being dealt but nobody appeared to be firing. So when a peer is firing (remember this is indicated by bit 4 of inputs), their PeerLaser is made visible and positioned from their gun to their aim position.

https://www.scirra.com/images/articles/mp3-ev72.png


The last events deal with the chat room functionality, covered in the previous tutorial. Finally the last event unfocuses the chat controls when we click in the game, otherwise our keyboard controls would go to the text box after sending a chat message. This way the user can click back in the game to restore keyboard control to the game, not the chat.

Conclusion

Designing multiplayer games can be challenging. These three tutorials have covered everything from the architecture of the Internet to how to perform lag-compensated hit-testing on the host, so hopefully now you are in a much better position to take on the challenge!

To learn even more, refer to the manual entry for the Multiplayer object. These tutorials have offered a practical guide to putting together each part of the Multiplayer object to produce a game, but the manual will go into more depth on each specific feature.

Designing multiplayer games requires a close awareness of where data is travelling, such as that Sync object only sends from the host to the peer, and client input values only go from the peers to the host. It is also important to remain aware that both the host and the peers are running the same Construct 2 project, which requires being able to consider your one project from two points of view. The technique of activating the correct event group makes this a lot easier to think about in the event sheet, but you must also remember this when adding the objects and setting their properties. With practice and experimentation this should become easier and you'll be able to design the multiplayer game you've been planning!

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

5
Tedg 9,889 rep

Awesome tutorial and awesome tool :)

Wednesday, March 19, 2014 at 5:21:07 PM
0
Jayjay 20.6k rep

Thanks for this tutorial! :D

Wednesday, March 19, 2014 at 5:51:25 PM
4
superwifibattler 3,332 rep

How do you make this with a drag&drop game or a board game or something?

Wednesday, March 19, 2014 at 7:02:12 PM
6
exertia 2,625 rep

@ashley - is there a way to restrict # of players to say 2 or a predetermined number, and force additional players into a new room / game.

Wednesday, March 19, 2014 at 8:33:47 PM
1
SergioRM 6,964 rep

really interesting! thank you!

Wednesday, March 19, 2014 at 9:59:39 PM
1
rexrainbow 156.6k rep

Like @superwifibattler said, the prediction might not be useful in some cases like turned based game (board game, poker game) , could Ashley provide some hint about that?

Wednesday, March 19, 2014 at 11:15:51 PM
2
Joskin 6,129 rep

Thanks for this tutorial ! It's not easy !

@rexrainbow, what about using the messages system for cards game ?
I can try to make a simple example.

Thursday, March 20, 2014 at 9:06:27 AM
3
rexrainbow 156.6k rep

@Joskin

Yes, I know it's possible.
My idea is -
1. [peer] request a command
2. [peer] validate this command (could I run this command?)
3. [peer] if validated pass, execute this command and send this request to host

4. [host] receive command , then validate it
5. [host] if validated pass, execute this command and send this request to other peers
- if validated failed , something with that peer

6. [other peer] get validated command, execute it.

The validation function and execution function will be the same at host and peer. (peer and host will use the same function to validate command)

Thursday, March 20, 2014 at 9:34:26 AM
1
potpie1010 2,836 rep

Awesome tutorial; great feature.

There's no other game engine that's fun to use, yet is still able to produce actual games.

Multiplayer should be interesting!

Thursday, March 20, 2014 at 1:18:01 PM
1
bilgekaan 25.8k rep

Great feature and tutorial thank you!

Friday, March 21, 2014 at 11:48:30 PM
2
dynamiczny 440 rep

Is there any chance of obtaining the code for the signaling server? After all I would not like to infinitely rely on the server which I don't even have access to..

Could you provide the code so that we could set up our own signalling servers?

Tuesday, March 25, 2014 at 2:30:26 PM
1
qu0y 3,151 rep

Thank you Ashley!

Saturday, March 29, 2014 at 2:09:01 PM
1
drappdev 2,164 rep

I think i missed the section around the room size.
If i change the "Max Peers" value to 2, and then open 4 separate browser windows to the localhost:50000 address. the first 2 i login are entered into the room, whilst the 3rd & 4th are given the error "signalling error: room full".
If anyone knows how to push the extra players above the max peers into 'new' rooms. I would really appreciate the knowledge share.
Thanks

Tuesday, April 01, 2014 at 10:08:55 PM
1
AbelaNET 14.8k rep

Ashley, thanks. Great info.

Wednesday, April 02, 2014 at 8:20:19 AM
2
Wsoukkachang 1,433 rep

@drappdev The new auto-join feature should help you with your problem.

Thursday, April 10, 2014 at 3:26:15 AM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.