0 Favourites

Multiplayer & Platform behavior - position syncing

  • Hello fellas,

    I'm working on a simple multiplayer platformer and having some problems with position syncing on peer side. Don't know what is the best way to implement it:

    Host & Peer both have platform behavior = problems with position syncing on a peer side, because multiplayer conflicts with platform behavior ( collision with solids). Apparent position glitches, shaking etc.

    Only Host has platform behavior = peer has quite a noticeable lag, almost game breaking.

  • Every time i see a post referring this I make the argument that for platform multiplayer games the current method of if interpolation doesn't work very well. What I do is sync instructions instead of objects and get perfect results. Its a bit more complicated but it works. Not realizing what I had developed after researching I found that what im doing is called extrapolation where the client predicts position based on state instead of plotting between two known points.

    Here is an example platformer that syncs instructions (function calling) instead of objects. Your welcome to try and adapt it. Just open it in a couple of browsers, click connect then use arrows to move players. You will see the synchronization is near flawless.


  • troublesum

    Thanks! Just tried your example and it works very smooth with no lags and glitches. Gonna dig into your events to understand how you did it.

    Could you roughly describe the main principle behind your implementation?

  • xoros Yeah is more complicated... Ill try my best to describe how it works (as a developer i can write pretty complicated/convoluted code)

    So to start out what I do is call functions simultaneously on all devices. I sync instructions not objects. Take a look at the Player event "Keyboard LeftArrow" You will notice i call two functions. One of them calls a function "MoveLeft" to move the player on my screen using a simulated action. You will notice the "MoveLeft" function takes a peerid as the parameter. This is so the function will for work any player (as you will see this is the same function each client will call as well using the the peerid they receive).


    The other function I call "Sync_MoveLeft". This is where the magic happens. "Sync_MoveLeft" packages up the function name "MoveLeft" and a peerid (which is my id since im the one that called it and loads them to a dictionary and then sends the dictionary AsJSON to all peers. Each peer receives the JSON string and loads it back into a dictionary on their end. They read the function name they are supposed to call and the parameters (peerid) they should pass to it and call that same "MoveLeft" function for me on there screen. All clients call the same function. pretty neat...

    But I found that the signalling just wasn't quite fast enough and the player would stutter a bit while moving. This where extrapolation comes in. What did was a create method for telling the peers to persist the function call on there end. You will notice in the Sync_MoveLeft function I also package up a persist value of 1. On the peers end they see this persist value and load the dictionary for constant use on their end. So every tick they will now call the move left command with the same peerid even if i don't send another. Poof! the player moves smoothly now.

    But now the player will move left to infinity so I need a way to tell it to clear that persisted action. So in the "Sync_MoveLeft" I also package up a persist tag (action tag really) with a key name "move" and the peerid of the player (more than one player could be calling move left at the same time). So what each peer does is when the see a persist value of 1 they check the perstsTag and if a persistTag already exists is clears it. So MoveLeft is persisted until MoveRight is called or stop is called. Bingo.. solid movement and precision control. If i call the same function twice thats not big deal because the persits tag will match and just keep using the same function anyway.

    Last just to account account for any potential lag I send a set position command every 30 ticks while I'm moving to ensure player objects don't drift to far out of sync while performing persisted actions.

    And that in a nutshell is how it works. Theres a bit more in there but I think if you follow the code along with the comments it should make more sense over time. Its just nice to know that you can achieve smooth movement you just have to do some of the heavy lifting you self and write some extra code.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • I think I understand it Thanks again, this part is crucial for my project.

  • troublesum

    That's amazing!!! I was finally able to get some multiplayer platformer, but I am still having a hard time to understand how these functions works.

    How could I synchronize an enemy's sprites movements between host and peers? I've already made Enemy Functions (EnemyMoveLeft, EnemyMoveRight...) and Enemy Synch Functions based on the previous ones, but each peer still see their own enemy unsynchronized.

    Thank you very much.

  • troublesum, why all peers have clones? Double spawn.

  • mdvgames - I don't think i understand your question? Are you having a problem?

    The idea is that when a peer successfully connects he calls a function "Spawn_Player" locally on his device and pass it his peer id and start location to create him self on his screen. He then calls a function "sync_SpawnPlayer" that will call that same function all other peers devices with the same information so now his peer object is created on everyone else's screen too. The sync message is really only sent to the host and then the host will broad cast it to the other peers so they call function he wanted with the parameters he passed.

  • troublesum

    Sorry for my bad english.

    Look at the example and count the number of boxes when we host and peers. Open a few browser windows and check number of boxes. Peers creates more boxes..


  • ah.. i see what your saying. I haven't looked at this in a while and didn't see that bug. Yah the problem is when the peer asks the host to send him all other peers, the host calls the "spawn_Player" function for everyone and not just the peer that requested it. I forgot about that problem and was why I stopped using this .. Trying to coordinate functions to "specific" peers was too complicated in the event sheet so i created a personal plugin that wraps the multiplayer and function plugins together so it does it all for me in a single call. On the example capx however you can just add a check to the "spawn_Player" function that if an object with that peerid already exists to ignore its creation.

  • troublesum

    Can you help me with this? I tried this, but I did not get.

  • troublesum

    Sorry to bother you.

    Maybe you have another example of an optimized platform multiplayer?

  • mdvgames - unfortunately i don't... The example I provided in this thread needs work to enable the ability for peers to call functions on specific peers instead of just being able to call them on all peers all the time. (and the process is complicated enough as it is) it was just a proof of concept i created but lead me to work on my plugin instead. You will need to hack it to correct for any imbalances. (Ie if peerid already exists dont re-respawn it.)

    I would release my plugin that simplifies the whole thing but i'm not supporting it as it hooks into official (multiplayer & function) plugins maintained by Ashley and as he makes changes to them could break mine and would leave your project broken unless i correct for any changes he might make which i don't want to be responsible for. .. If you message me ill provide you a link to download it for personal use but to use at your own risk that someday it could be broken and I might not be around to fix it.

  • I found that stop using sync - is the best way to make smooth and undercontrol game.

    Just send to host all inputs + id variables, and then broadcast to another peers.

  • You sir, are a god! thank you so much!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
1,068 148,700
Reflextions's avatar
Unread hot topic
0 Favourites
[BEHAVIOR] Chipmunk Physics
673 82,515
nelostic's avatar
Unread hot topic
0 Favourites
[Behavior] LiteTween
672 169,920
manujorgo's avatar