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,085 visits, 51,621 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

Multiplayer - Handle object creation

Send the Master Client data to all players to tell them where they can create their own local copies of all player objects

Finally we come to the really interesting point: Sending and receiving events. As we have seen in the on actor join event the Master Client is sending his local dictionary to all other players with the Raise event action of Photon. The Photon cloud now delivers the event to all the chosen players and if a matching On event with the correct number is found this event will be triggered.

First add a new On event from Photon. You can now decide which number it is. Keep in mind to ALWAYS have different numbers for your events. Never use the same number twice to prevent confusion and errors. For now put a 1 into the field. That will be our event to create all players from the dictionary received from the Master Client. The On event holds some EventData which can be accessed by calling Photon.EventData. That's basically it to receive any kind of string data from other players. Awesome, eh?

We gonna use the EventData now to update our local JoinDataDictionary. Add a new action in your event 1 go to JoinDataDictionary and select Load. Into the textfield put Photon.EventData. This function tries to load a Construct 2 dictionary from a JSON encoded string by decoding and converting it.

Now it's time to loop over our dictionary. Remember our dictionary holds the player unique id's (ActorNr) as keys and as a value of every key an Array with the goto, shoot and spawn status data. Select *For each key from your JoinDataDictionary add a new subevent with the condition to check if a player object with the same id as the current iterated dictionary key is found. It is IMPORTANT here to remember that our id is an integer. The keys in a dictionary although are strings. So we need to convert our dictionary key to an integer first (Tell me if i'm wrong here but it didn't work for me when not converting it). We do this check to make sure that the player object is not created twice. So if a player object is found we simply do: Nothing!

Instead we add an else (press X) and handle the creation here. To access our current value of the current iterated key Construct 2 offers a nice way by going to the JoinDataDictionary and pick the JoinDataDictionary.CurrentValue. We gonna use this value to initiate our JoinDataArray. So go ahead and add the Load action for the array and input the CurrentValue. Add a subevent now and add the following condition: JoinDataArray -> Compare at X. Input 4 as the X value (means position 5 in the array) and check if it's equal to 1. This check is very important since it tells the player if this player is allowed to be created right now. If the round is running this value would be 0 (zero) so the player wouldn't create the object and that's what we want. For now we pretend the round isn't running. So we gonna add our CreatePlayer action and input all the data from the Array. Create another else and add the condition to check if the actual key (unique player id) is matching to my local id. If yes tell me that the game is running and that i need to wait.

  • 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! :)