Full round-based multiplayer game with Photon

12

Index

Attached Files

The following files have been attached to this tutorial:

.capx

photontutorial.capx

Download now 184.34 KB

Stats

20,041 visits, 51,542 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 9 Feb, 2017. Last updated 19 Feb, 2019

Add error catch

The first step for now is creating a error catch event in case something's going wrong. Add a new event from the Photon object and choose On error. In this event add a new Function action. Go ahead and choose Call function. Type in "Debug" and add a parameter. This parameter is the text that is displayed in the TextBox. In the parameter type in or choose from the expressions list Photon.ErrorMessage.

Connecting to the lobby

First thing we need to do is connecting to the name and master server of ExitGames. To do this we create a new event in our Event sheet. Add the On start of layout event from System. Create a new sub-event. Copy the "Debug" action from the On error event. Change the parameter to "Connecting to main server". Now create another action from the Photon object and select Connect. That's it for connecting to the main server.

When connection is successful the player should join into the lobby. Add another Photon event called On join lobby. Copy the "Debug" action again into this one and type as parameter "Connected to lobby".

Connecting to a random room or create it if it doesn't exist

To join into a room we make it easier for us by adding a new Keyboard project to the project. Add a new event for the Keyboard and choose On key released. Pick whatever key you like. I chose key "1". Add another condition to the event from the Photon object called Is connected to a lobby and another one from the Photon called Is joined to a room. The player can only be connected to either a room or the lobby, not both at the same time. With this done add a new action into this event and call the Join random room action from the Photon object. Leave everything as it is and press done.

Next we add the "exception" event for the random room event. Add a new event from the Photon object called On joinRandomRoom no match found. Add a new action to this event from the Photon object called Create room. Leave everything as it is and press done.

Finally we will add the On join room event from the Photon object to make sure we are connected to a game. In this event copy the "Debug" log again and type as parameter "You connected to a game" because this event is triggered only for the player who's joining the room.

That's it. We should be connected to the server and being joined in a room.

Leaving a room by pressing 2 on the keyboard (for testcase)

First create a global variable called meIsExiting with default 0 (zero). This variable will be set to 1 whenever we decide to leave a game and rejoin to the lobby.

We simply add another Keyboard event like before but this time we pick key number 2 to trigger the event. This time it's necessary to check if we are connected to a room instead of the lobby to only trigger the event when we are in a game. We set the meIsExiting variable to 1, trigger the Leave room event and display a message for the player. The meIsExiting variable will be explained in the next steps.

  • 6 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • it won't let me chance gotoX/Y to Self.X/Y

  • First player connects to the room, but the second one doesnt

  • how to add ms(Ping or latency?

    for ex, (68ms)

  • (on data has sending will add 1 to enemy score and display it) When "collision" is, then the score is -2 then how will it be (on event 1)? I am trying too many times.so please help me...i am beginner

  • I know this is 2 years old already, but WOW!

    You are so amazing... thanks for helping me with this! Especially the part with sending a whole array to another player was really frustrating for me, I would never use Dictionaries to solve that problem, thanks for that awesome tutorial.

    I was struggling with the syncing of enemies in my game, finally got it now! :)