Multiplayer Help: How do I sync host/peer global variables?

  • I'm just learning how to do Multiplayer and I am having difficulty getting the host and peer to constantly exchange their global variable information or something of the sort.

    I am using the Pong program as my reference, but I am still missing something. What I am trying to accomplish in my test program is, have both the host and peer be able to share all their variable states (user name to start) and interact from their exclusive layout pages. Ultimately I am trying to make a card game. Your own mobile device would show only your hand of cards and the played cards would be visible and common to both host and peer.

    Right now my program will show the host name only on the host device and the peer name only on the peer device. How do I get them to share info an update continuously? What am I missing?

  • In reference to sharing variables/information between players, I'd create an invisible object that's created when a player joins (both Host and Peer), and is associated with that player. Each player's info/variables would be added to those objects. Additionally, to sync that information, just follow the multiplayer tutorial and sync objects, as well as their instance variables (similarly to how Input is synced in the examples).

    For any text based data, you'll obviously want to send that as a message between the users. If I were you, I'd store each user's hand (deck) on the host's side, regardless of where it is seen. Store each user's hand/deck in an array. When a value on each changes, send that array to the peers. For example, if a card is added/removed from a players hand, send a message through Multiplayer "'UPDATE_HAND', *HandArray.AsJson*". Make an event for when that message is received, set the Peer's array to the JSON in the message.

    If you have any trouble, let me know. I don't have Construct 3, so I can't open your project.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi, I'm making a similar game, I realized working with multiplayer stuff is much easier than I initially thought. And yes, this was the very first challenge: Send your name to host/peer, and retrieve his/her name.

    This can be done through "Send Message" option of Multiplayer, and likewise, "On Message received"... In fact, my whole multiplayer game uses only Sending and Receiving messages to communicate, and it works perfectly! (i.e. as long as it is a turn-based game)

    So, let's say, when you type your name, On button pressed, you can choose > Multiplayer > Send Message

    (this will be done in common, right? whether it is the host or peer), so you need to include a sub event there:

    if host > Send Message (to Multiplayer.peerID, tag: "name", message: name.Text or whichever)

    else > Send Message (to Multiplayer.HostID, tag: "name", message: name.Text or whichever)

    Then, in the Host Group only, have a condition:

    On receive message "name" > Set oppname to Multiplayer.message

    And likewise, in Peer Group, have similar condition:

    On receive message: "name" > Set oppname to Multiplayer.message

    If you can do this, you can communicate anything from peer <--> host by just sending "messages" with information, throughout your game.

    Let me know if this helped.

  • even if the layout is diferent? my issue is that in my game the layouts are diferent.... how do i makle? im having issues in sending and reciving data.... i need a screen where only the host can see, in this case a diferent layout.... how do i make that constantly both ends check the global variables, i dont need to check any kind of local or anything just points that ive wanted to send to the host and to my tv witch has an computer

  • Note that host and peers that are not on the same layout are already desynced. Changing layouts destroys all objects and makes new ones for the new layout, presumably different than the ones from the previous one, so the host and peer will have different objects at different positions.

    Sending messages to update global variables should work fine as Hasan999 describes though.

  • yes but i could not make this work, im having trouble only on this part.... ive tried to send and recive mensages but no luck

  • yes but i could not make this work, im having trouble only on this part.... ive tried to send and recive mensages but no luck

    Then you should post your project as it seems you do have an issue on the implementation.

    The provided theory is indeed correct, exchanging messages in an asynchronous way does work with the Multiplayer plugin.

    You need to set up a clear "Host" part of your game and a clear "Peer" part.

    And have sending and receiving messages clearly stated, according to the "part" you are currently executing.

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