Problems getting variable to replicate in animation blueprint

So I have a character controller where I’m grabbing the Axis value of InputAxis MoveForward and InputAxis MoveRight and passing them to a variable which then controls a Blendspace in a Blueprint animation:

(Note that I multiply by Walk Speed to better sync with the animation blendspace thresholds)

In the Animation Blueprint, I then cast to my character blueprint and grab the two variables and use them to set speed and direction in my blendspace:

This works fine for single player, but it seems that not matter what I do, I cant get these two variables to replicate on both the server and client.

I’m relatively new to networking but I tried (unsuccessfully):

**1.) **Setting both variables to be replicated

**2.) **Grabbing the axis values via a Custom Event set to Run on Server.

**3.) **Messing with Has Authority to set both variable, with little luck.

Any help or feedback is much appreciated, thanks!

Answerhub Link:

Dont think its as easy as that, those variables have to be set BY THE SERVER in order to be replicated for every one else…

Yes, and the problem seems to be that InputAxis values cannot be set by the server for some reason, unless i’m missing something

Hey Robert! There are a few things which aren’t right here replication-wise.

-This is all happening in a PlayerController. PlayerControllers only exist on the server and the client which owns them, which you’ve probably gathered.

-The InputAxis values are local only - they will be 0 when accessed from any machine that isn’t the one physically pressing the button. Even so, the event will (I believe) still get called on the server’s version of the PlayerController - just with a value of 0.

-Here, you’re attempting to set ForwardAxisAmount, a replicated variable, on the client. Replicated variables have to be set on the server - the nature of a replicated variable is that the server will constantly update all the clients with the server’s value.
So when you set ForwardAxisAmount on the client, it is immediately corrected back to 0 - the server’s value of MoveForward*WalkSpeed. If you want to set it on the server, you would make a Server RPC, like this:

Here, you’re passing the client’s value through to the server (it’s important that the Set node is getting its value off of the ServerSetForwardAxisAmount event, not the InputAxis MoveForward event - otherwise you’d just be telling the server to set ForwardAxisAmount to its own MoveForward value, which is 0).

However, even this won’t work, because this is happening on tick on both the client and the server - every tick on the client machine, the client tells the server to set ForwardAxisAmount to its MoveForward value. Meanwhile, on the server machine, the server is doing the same thing, constantly telling itself to set ForwardAxisAmount to its MoveForward value - which is 0, because no physical input is being received on this machine.

So, we’ll hide the whole thing behind an Owning Client RPC:

So this is saying, only run OCSetForwardAxisAmount on the owning client. On the client machine, we’ll hit that node, go “okay, I am the owning client, so this is fine” and move ahead to ask the server to set the variable to the value we want. If we hit OCSetForwardAxisAmount on the server, we’ll do nothing further on the server, except tell the client machine to run OCSetForwardAxisAmount.

I could be wrong about some things, although that has only happened once before. I’m not 100% sure that InputAxis nodes still exec even on non-owning clients. It would be good if they didn’t.

Further to this whole thing, though, you’re probably going about what you’re actually trying to do in a bad way - ideally, your AnimBlueprint should need little-to-no information about the actual input that’s happening, especially if that suddenly means you need to constantly send an input value over the network. Instead of having your blendspace key off of ForwardAxisAmount, it would be better to go off of the actor’s velocity - something that you can be sure you always have access to without network shenanigans. Even if you make this approach work, you’re unnecessarily trying to do a server RPC a bunch of times per second.

Also, the more your AnimBlueprint is kept church-and-state from your input and your replicated classes, the easier it will be to keep that animblueprint when you try to do other things with it - e.g. put that animblueprint on an NPC and have it work right without any changes.

Hope this halp

Hi Joe!

Thanks so much for taking the time to break this down into such an easy to understand format, it’s help me understand some concepts that I was struggling with previously.

Based on your advise, I came back to it fresh this morning and created an animation blueprint which relies on velocity instead of axis inputs. One aspect I struggled with initially was separating velocity into right and forward axis values and then getting a positive or negative vector based on the direction travelled. However, with a bit of sleuthing I found that doing a dot product between velocity and actor forward/right vectors had the desired result:

Hopefully this is a more sensible way to do this. Let me know if you see any flaws or have any feedback on my results, I’m eager to learn! :slight_smile:

Looks good! Happy to help