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.
Add UPROPERTY(Replicated) above your variable that you want to be replicated.
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.