How do I sync animations in multiplayer?

0 favourites
  • 7 posts
From the Asset Store
3 pixel charaters with over 8 animations each + animations with a gun
  • The Problem

    I've got two players in a platform game environment. The Peer can see the Host moving around, but can't see them animate, so they're stuck in the Idle position as they move.

    My method

    Am I going the right way about this?

    Each character has a currentAnimation instance variable attached to it, and the host is updating the variable whenever the animation changes, like so:

    + HeroBase: playerID = Multiplayer.HostID

    + Hero: characterName = HeroBase.associatedHero

    + Hero: Animation frame = 0

    -> Hero: Set currentAnimation to Hero.AnimationName

    -> testText: Set text to Hero.AnimationName

    I'm using that testText in order to check that the instance variable is updating on the Host's screen, which it is.

    I've synced currentAnimation like this:

    -> Multiplayer: Sync Hero variable currentAnimation (precision Normal (float, 4 bytes), interpolation: None, client value tag: "")

    And on the Peer's side, I've tried to translate that instance variable in to an animation for the Host's character:

    + System: Every tick

    ----+ HeroBase: playerID = Multiplayer.HostID

    --------+ Hero: characterName = HeroBase.associatedHero

    ---------> Hero: Set animation to Hero.AnimationName (play from beginning)

    ---------> testText: Set text to Hero.AnimationName

    But the testText on the Peer's side just keeps showing "Idle" and the Host is just floating around.

    The question

    Is this the right method to keep animations synced? And where have I gone wrong please?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Is hero.animationname a string? Sync instance variable doesn't work with strings iirc.

  • Is hero.animationname a string? Sync instance variable doesn't work with strings iirc.

    Just checked with the documentation, this is right, oosyrag, thanks.

    What would be the smartest way to ensure the Peer sees all the same animations that the Host sees?

  • You have the right idea, just use numbers to represent animations.

    A simple way would be to use an array that contains the names of your animations. The index number would be what you want synced. You would be able use indexof("animationname") to get the index number, and array.at("indexnumber") to get the animation name. There is a minor inconvenience of updating this array when making changes, but generally speaking it should be a once and done type thing.

    Alternatively, you don't need to sync animations at all. This could actually be preferred if your game allows for it. Instead of setting animation states based on input, you would set them based on the result instead, locally. This could result in more consistent results in some cases.

    I consider it best practice to only sync what is absolutely necessary to gameplay. Everything else is an illusion - there is no reason to sync what the peer and host sees, because peers will never see the same thing at the same time to begin with. Multiplayer development is all about designing around how to hide this. If you can get into the mindset of making the game look good and play well from each person's own perspective, rather than having the peer and host see the same thing (admittedly the natural thing to want), development will go smoother.

  • Thanks for all the advice oosyrag, it's really appreciated.

    Alternatively, you don't need to sync animations at all. This could actually be preferred if your game allows for it. Instead of setting animation states based on input, you would set them based on the result instead, locally. This could result in more consistent results in some cases.

    My first thought for this is to send a Message to the Peer when an event happens, and have a listener on the Peer's side that initiates the animation when the Message is received. Is that what you meant by that?

    It's a 2D side scrolling game where the two players share the screen a lot of the time, so it's important the Peer can see the Host's animations rather than seeing them float around.

  • No messages needed. Since movement is already synced, the peer can just compare the host position to the previous position to see if the host is moving or not (in the air or not, ect), and play the correct animation. This gets a little trickier for attack animations though, depending on what you need.

    Or just store host inputs as you would a peer's in an instance variable, and sync that. Then the peers would have the host inputs, albeit a little delayed, and can apply the proper animation based on input. This would be the most straightforward method.

  • Really helpful, thanks so much!

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