I’m trying to add networking feature to a side scroll character. Currently I have only a climbing wall module added to default template. Now for making it network ready I should set all the variables on server side so server can see the client character climbs a wall.
I used 20 variables for this simple climbing and now I should set them in server which means I should add a server call custom event for every variable that I want to set. So I will end up with 20 events as well. The question is “Is it a tedious job or I’m doing this wrong?”
Actually I forgot to mention that I need two custom event for every variables. One for telling the server to call another multicast custom event which sets the actual variable.
40 Custom events in total !!!
Hi @mightyenigma. Yes, but it’s really a confusing concept. Let me ask a better question. When I set a variable in a RUN ON SERVER event which should I use? A replicated variable or set it on a multicast event again? what is the cons and pros?
Replicated variable will automatically copy the value from the Server’s version of the world to the Client’s version of the world when the server feels like it, which is usually after anywhere from every 0.1 to to every 0.5 seconds. This happens without any scripting, just tick the Replicated checkbox on the variable.
This does not go back the other way from the Client to the Server though.
Multicast will execute its function immediately. The Server tells all the Clients “RUN THIS FUNCTION NOW” and they do it as soon as they receive the data packet from the server (which will take the one-way server-to-client ping time to happen). It will pass any number of variables along through the event that you decide to put on it.
Should you do this every tick? NO WAY. That will stuff your network connection full of RPC calls and leave very little time or room for normal replicated variables to take their turns or even other RPC calls, effectively slowing things down a lot.
Do Multicasts when something has to happen immediately on all machines.
Let variables automatically replicate on their own when they need to update on all machines one-way from the server to the clients, and it can wait up to a half-second and not ruin the gameplay (you can actually tweak the update frequencies and priority of replicated variables, etc but I’ve never tried that).
You want to avoid driving things from the Client to the Server whenever it could be done just by asking the server to please do it for the client and replicate the results back to the client or client(s), although sometimes you will want to do it on both machines and then let the client be “corrected” by the server via the automatic replication in order to synchronize their versions of reality.
RepNotify is weird – it notifies the client that the variable has replicated and runs a function on the client as a result, but doesn’t run it until the variable or actor it belongs to becomes Relevant on the client machine.
I can’t tell if you’re doing it “wrong” because I’m not sure why you need 20 variables to copy their data to all the clients to get the same effect. Maybe you really do need to, but usually with stuff like that, you just trigger the clients to run the animation themselves and give them only the data they need to make sure they do it the same way as the server did, which sometimes is nothing because it’s a pre-set thing that happens the same way every time.
When I change my character’s custom appearance, however, I use the settings stored on the client, so it has to pass along the variables that were changed in a Server RPC, which then changes it on the server and then has to tell all the other clients to change that pawn’s appearance on their machines, to look like how the owning machine set it when it started that whole relay of info. so that would need a Multicast that passes the same variables back down the line. The one who sent it would receive that Multicast too, so it can probably not do the change on its end except inside the Multicast too.
So for that process I did this:
Client Z send to Server S a reference or integer or something indicating which model it wants to use on its character.
Server S receives the value (let’s say it’s an integer) in a reliable Server event, passes it along to a Multicast that has the same parameter.
3.Multicast runs on Server S, Client Z, and Clients A,B,C, and D. All get the integer value originally sent by Client Z (including client Z). and use it to select a character mesh from an array that’s the same on all the computers anyway because it was initialized the same way because they’re all running identical copies of the game’s code. So the new model gets selected on everyone’s machine and all you had to send through was one little integer variable.
I think this approach can be used to replicate animation changes as well, in many cases, but not all.
If you mark a Server, Client or Multicast event as NOT reliable, then it can choose not to send the RPC in order to avoid clogging up the network traffic if it’s got a lot of other things to replicate. I do this when the Multicast triggers sound effects or special visual stuff that doesn’t affect gameplay very much or can be caught up on by the next automatic variable replication later in a few dozen ticks without ruining the experience for anyone.
@mightyenigma, I understand this very well now. I wish I could hug you. So for clarity I have a situation here. Lets assume I have 6 variables that will handle state machine rules, so I need to replicate them along because every one should see the animations and every other variables that sets my character position too like am I flying or my capsule moves with a function.
But right now I want to use a Move Component To to move my capsule in a server event and my client lag when its moving, by the thing that you told I think I should move my character locally and tell the server to move it for other clients, am I right?
If you’re using a Character class, then it has some nice built-in network movement smoothing and prediction (without that, movement ALWAYS looks choppy no matter what I try). I think you wouldn’t need to use Move Component To, just Add Movement Input.
There is still a lot I don’t know about the CharacterMovement Component but it’s the only thing that has given me smooth replication of location changes both to server and clients.
The tricky thing about Character movement is that it automatically tells the server about the Client moves without you really having to do anything (as long as you use Add Movement Input) or adding your own RPCs. It only does that for the location and rotation changes but that’s the hardest part.
To replicate changes in Animation then I think you do need the Server RPC → Multicast RPC setup.
So anyway just try using Add Movement Input instead of Move Component To if you have a Character type of Pawn that has a CharacterMovement component.