Download

Network programming - a lot of functions and code or something is wrong?

Hello friends,

I’ve started working persistently on my project game and it seems that writing RPC functions creates a lot of extra code and functions. I am wondering if this is right or am I doing something wrong. So far in my character I have 3 things that needs replication. Health, Sprinting and Changing the speed of the player.

So i have 4 functions to replicate the health (SetHealth and ServerSetHealth() + Validation and Implementation). 4 more to change the bool bIsSprinting and 4 more to set the speed of the character when key is pressed. That resulted in 4 * 3 = 12 functions. They are not complex or anything but as far as I know, when a function is called it takes a memory. So if I build a lot of functions wouldn’t that result in a lot of memory taking? Or am I doing something wrong?

I’ve been thinking on improving a little bit the bool and speed of the char like, whenever I set the bool in the RPC function, I set the speed as well -> solving two problems with one function hehe. But is it ok for the code?

It sounds like you what you are looking for is Replication.

You are achieving replication through RPCs, which also works. If you want to clean up the code a little it’s simpler to set the variables to be replicated.

  1. Add UPROPERTY(Replicated) above your variable that you want to be replicated.
  2. Implement GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
  3. Add DOREPLIFETIME(AReplicatedActor, bIsSprinting); in GetLifetimeReplicatedProps

In order for a value to be replicated, it needs to be modified on the server and is then replicated to all clients.
UPROPERTY(ReplicatedUsing=OnRep_Flag) also exists, which will call OnRep_Flag() everytime the variable is replicated.

Yeah I figured it out, the code get a lot more clean with replicating variables. Thanks