Passing client set variable into Run on server RPC

Hey everyone, I’ve had this little thought that’s been on my mind about RPCs. Is it a good idea to pass client set variables into a Run on Server RPC? Part of me says no because the client can then just pass in any value they want and the RPC will take it. But the other part of me isn’t sure. Any opinions? Thanks!

Yes, you are exactly right. Sending in a value to an RPC and then just having the server accept that value will make your game prone to cheating. You have to remember that the client is capable of manipulating anything that the client has control of. In fact it is possible the client that is connected to your game might not even be UE4 (this is an extreme case, but is not impossible).

Here is an example to think about. Let’s say that you are programming an attack action for your game where your player character swings a sword at an opponent.

A bad way to handle this would be to have the client swing the sword and see if it hits the enemy. Then if it does, send an RPC to update the health of the enemy. While this might give you the result you desire, it would be easy to cheat. I would hack this by just changing the number that is sent for the health in the RPC call to either a really large number or zero and then your game would be broken.

A better way to handle this would be to send an RPC call from the client to the server when the client presses the attack key. Then handle all of the collision checking and health deducting on the server side. If you do it this way, it is much harder for a player to cheat, since all they can cheat with is a key press, which is something they were allowed to do anyway (unless there are cooldowns, but you can check for that on the server when you validate their RPC request).

When it comes to RPC’s in multiplayer games, think of them as requests to ASK the server to do something, instead of commands to tell the server what to do.

Okay good so now I know that what I’m doing for one of my functions is wrong. I’ve been trying to implement this movement ability which is pretty much like an aerial glide.

The way it was supposed to work was that the game would check for a boolean to see if the player was trying to do the glide and then it would set the velocity of the character to move in a certain direction for a period of time. The issue I was having was that I think the server was constantly correcting the client when I tried to activate the ability on the client’s side.

I tried doing a server RPC without passing in any variables and it didn’t work still, then when I passed in variables, it worked. But again that solution has some pretty major flaws. Do you happen to know the reason why my Server RPC without variables wasn’t working. If you need more details I would glad to elaborate. Thanks!

RPC’s do not have to have parameters to be replicated. But they do need to be set to “Run at Server” and in your case you want the “Reliable” checkbox checked. Also, the RPC call should be made from an Actor owned by a Player Controller (I am assuming you are using a class derived from ACharacter). I found out the hard way after a lot of frustration that RPC calls from un-owned actors do not replicate.

It sounds like you should be able to call a server side RPC from the client when the Glide key is pressed (or whatever other action starts a glide). Then on the client side, set the velocity of the character. Let the server side movement replicate back to the client. Don’t set the velocity on the client side at all or they will fight like you noticed.

One of the things that there isn’t a lot of documentation about is the network movement prediction system. This is part of the Character Movement component that comes as part of the Character class, but not the Pawn class. This always confuses a lot of people. The network prediction system is fairly complex and allows movement to be synched up between the server and the client in a way the appears smooth on the screen. However, it also means that if you try to move your character on the server and the client, you will often run into jittering or stuttering.

Yeah I was implementing the glide function within a custom CharacterMovementComponent after finding a guide on the UE4 Wiki. Had to do a few modifications but it was helpful. But it would be nice to get some official instructions from the UE4 devs just to make things clear.

Just to be sure though, in your second paragraph, you said “Then on the client side, set the velocity of the character.” By that do you mean set the client’s velocity within the server RPC? Or did I miss something?

Sorry that is a typo.

“Then on the client side, set the velocity of the character.”

should be

Then on the server side, set the velocity of the character.

Oh alright that’s good. I’ll take what you said into consideration when I get back to the issue. Thanks a lot! Have a great new year!

EDIT: In case anyone else wants to chip in, feel free to comment on the post!