Multiplayer: How do you sync a variable between all players?

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Before I start, I would like to mention I have gone through all multiplayer tutorials and read through other multiplayer threads on the forum, however I have not got this to work.

    My steps so far have been starting with the top down multiplayer example. I added a variable on the character called teamID. This variable starts as zero, but before the player joins a room, it is assigned a 1, 2, 3, or 4. After connecting, however, the host immediately sets it back to zero.

    I have looked at the other variables in the example and tried to mimic them, but for some reason still does not work. Each player will have their own variable, which all other players will see. What steps would you take to implement this?

  • A synced variable must be set by the host, not the peer. Syncing is one way, the host does not receive any data from syncing.

    Also, the peers' character should not exist until after joining the room, the host needs to create it for the peer. The host can then assign the instances variable upon creation. This will allow both the player character and instance variables to be synced to all peers.

  • Thanks for the reply, I did simplify my example so I think I will still run into problems. I do know the syncing is one way, but I didn't mention that each player is choosing their team, so the host absolutely must receive this information before assigning it. There is also a variable I added for character selection which the host cannot randomly choose.

    Is there maybe a way to have a peer join the room, the peer then sends these variables to the host and is temporarily saved by the host...the host then creates the peer, sets the variables, and then finally syncs?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I was about to ask another question about the syncronisation of variables when I saw this topic.

    I had a similar issue just 2 days ago. Just scroll down to my last reply.

    I don't know if it will help you but what did the trick for me is to set the sync object and sync variable events on the host side as well as on peers side too. Before it didn't work for me, but after this it works now.

    Either the syncing must be initialized on both sides or I did something wrong. But either way the documentation is a bit confusing and needs some more clarification or an example on how to set this proper up.

    Also it is kinda sad that syncing variables doesn't work for strings.. .

  • Have the peers send a message to the host.

    Instead of creating the peer object upon peer joining, you can do it on message received instead, with the selection in the message. So on message received, the host will create the player object with the correct peerid and team instance variables with the data available from the message. This will then get synced along with the instance variables to each peer.

  • Unfortunately the peer id is a string and not a number and will not be synced.

    I believe that this is the order of things to get it synced correctly:

    1. Host and peer both define the objects that need to be synced.

    2. Host creates the objects that need to be synced or their variables that need to be synced with its owners (players) multiplayer index in a local variable (not id). You get a joining players index with Multiplayer.PeerCount-1 or you loop through every peer and check if the id is matching and the index will be the loop index -1.

    3. Peer: On (synced) object created send a message to the host with this objects local multiplayer index, basically asking the host for information about the player with the given index.

    4. The host then uses this index to get and send all of the information back to the peer. The easiest way to do so would be to use JSON.set all of the information and then JSON.ToCompactString.

    .

    5. When the peer receives this message it will loop through all of the objects and check for the local index variable. If it matches the one from the message it can paste all of the data like id, name, level or whatever you just sent to the objects local variables. Do not forget to parse the JSON data from the message. Then use JSON.Get("").

    .

    This is a bit of a back and forth. If somebody knows a more efficient way please let me know. :D

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)