Problem sending multiple messages with multiplayer object

0 favourites
  • 9 posts
From the Asset Store
Be quick and choose the right answer for the shown equation.
  • I'm doing my first tests with the Multiplayer object of C3, so I'm not very expert with its functioning.

    At the moment of the connection between host and peer, the host creates a grid of instances of block object. I would like the connected peer to in turn create the same grid following the same pattern as the host's grid.

    So I thought of sending a message to the peer with the data of each block to make it create it in the same position:

    The problem is that the peer correctly receives the coordinates of the first block, and then receives a number of messages equal to the entire host loop, containing the coordinates of the last block of the grid, i.e. the last block created by the for loop.

    At this point I can't understand if C3 is able to send messages correctly in a short time. Or rather: the messages are sent, but the values inside are not updated correctly.

    Can anyone tell me where I'm wrong or if there is a better method to recreate the same object partner for both host and peer?

    Thanks!

    Tagged:

  • I was testing all day to figure out where the problem was, but I couldn't.

    However, I made a discovery: if when the peer receives a series of messages, it has in its message receive event an Alert, only the first Alert is correctly displayed with the right Multiplayer.Message, the other Alerts, are all read with the content of the last received Multiplayer.Message.

    If the Alert is removed, everything seems to work correctly.

    Since my explanation is not very clear, I have modified the official C3 chat example to reproduce this problem. Just go to the Peer window, select the text field and press Enter. It will automatically send 10 messages to the host via a for loop.

    File: dropbox.com/s/fo11q00fwsjihux/Multiplayer%20chat%20example%281%29.c3p

    Sorry to tag you Ashley, but I think only you can give me a correct answer and help me.

    Thanks in advance!

    Edit:

    I understand that if I insert some particular event (like "Alert" or "Wait" events) into the read event of the message sent by the host, the event fails to queue and read more than one message properly.

    Is this normal or a bug? If this is normal, is it possible to understand why and how can I work around this problem? For example, if I want to insert a wait between the execution of multiple messages with the same tag, how can I do that?

  • Don't use alert or anything else that waits when receiving network messages. It means that while it's waiting, other messages can arrive and overwrite the current message. It should always work fine if you avoid alerts/waits.

  • I understand, so my suspicions were right.

    If I need to insert a wait between one message and another with the same tag, I must necessarily save all the values in a list first, and then run each message by cycling through the created list. Right? Or are there better methods?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Don't use alert or anything else that waits when receiving network messages. It means that while it's waiting, other messages can arrive and overwrite the current message. It should always work fine if you avoid alerts/waits.

    I continued to experiment with the functionality of the Multiplayer object, and found another problem that was a bit stranger than the one in the previous posts.

    I'm attaching an image so you can better understand my situation:

    (the two functions set variables and create objects. They don't have any event of "Wait" or similar)

    The events you can see are executed only by the peer, and the various events of sending messages to the host are executed when your Player objects have been correctly created (the sub-event of the "On created" event is executed only when the second Player object (there are 2 Player objects in total) is created.

    The problem occurs randomly, as some times everything is executed correctly, while others I've noticed two types of errors occurring randomly and separately:

    - In the first sub-event, the two functions are not executed, but the "pg" message is still sent.

    - In the event of receiving the "pg" message from the host the Player pick is not executed because through an Alert of Player.Count the result is 1 (this should not be possible since the previous message of the above event is sent only if the second Player has been created)

    By inserting those two "Wait" events it seems that the errors occur less times, but still they occur very often.

    Since some times the code works correctly, I can't figure out where the error is and what it is caused by.

    I thought that the ids present in the Multiplayer object sometimes were not correctly accessible, and I tried to save them in a variable, but the result did not change, so the problem does not lie there. I have also checked several times that the Player.peerID variables are correctly assigned, and each time the variables are correct.

    Finally, I would like to point out that every time the problem occurs, both Players are on screen correctly, although a few times the Alert with Player.Count returned me the value 1.

  • I'm not following what you're trying to do in your last post or what your issue is very well, but I can give you the general advice that besides avoiding any hardcoded wait (waits should never be necessary, if you're thinking with a proper multiplayer/latency mindset) you don't want to use a loop to send multiple messages with the same tag. Loops are executed all on the same tick, so all of those messages will be sent simultaneously. You'll probably want to either send the messages one at a time per tick, or use loopindex in the message tag to differentiate them, so that when they arrive at the peer they won't all overwrite each other.

  • As for my first problem, I solved it by sending a single message at the end of the loop in JSON string format. This way I avoided sending messages to the peer for each loopindex. Sending each message with a tag based on the loopindex would have been very difficult since if I have a 10x10 grid I would have had to hand write 100 ifs to make the peer perform the correct action for each message tag received.

    My big problem that I can't solve/understand now is the one mentioned in my last post.

    It seems that during message exchanges the two apps lose their synchronization....

    When a message with a certain tag is received, can it be overwritten by another message with a different tag received shortly after?

    As you can see from the last screen, I created a "dialogue" of messages with "pg" tags between host and peer so that one sends data to the other only after receiving the right information from the other. In this way I thought to avoid loss or lack of data, but it doesn't seem so. By removing the 0.01 sec "waits", the problem seems to occur more frequently.

  • Combining all the data together into a single message is an excellent solution as well, I should have mentioned that.

    You normally wouldn't have to worry about overwriting of messages especially if you use reliable ordered. Messages will be received in the order that they are sent, and you should simply have triggers to handle them immediately when they are received.

    There may be an issue with your functions and parameters. I'm also not understanding what you're trying to do with the browser alert.

  • I think I was able to solve it.

    The problem was not in my functions, but in the way C3 synchronizes the objects. The Player object that was specified as the object to keep synchronized with the peer, was not always created in the same sequence. In practice, I noticed that although Player1 was always created first in the host, when the instances were synchronized in the peer, Player2 was often created first.

    Currently, however, I think I have solved everything by setting the sending phase of the various data by step. Each time all the objects have been created correctly in the peer, the peer sends a message to the host to indicate that everything has been created, and consequently the host proceeds to send the rest of the data.

    Thanks for your answers though! ^^

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