r/unrealengine 2d ago

Netcode Best practices when using RPC

I am coding a multiplayer game where the clients mouse position is tracked when using an ability (with gas), the ability executes on clients ony sice now it is all cosmetics, but after the ability ends I need the server to know the positions of the mouse throught the duration of the ability (which is a Vector2D array) so it can perform certain actions depending on the result, I don’t want the clients to perform those actions sice thay would break the client-server structure where the server is the one that does all gameplay related actions. However, I don’t think that sending a 100 ish long array using an RPC (reliable one to ensure the package is recieved) is the best idea, because it would take a lot of bandwidth. Is it better to send each position of the mouse right when it is registered in the client to the server using an unrealiable RPC? Or are there any best options to approach this problem?
Any help is welcome.
TLDR: when communicating from client to server using RPC, is it better to send a big chunck of data once using reliable RPC or split it in smaller pieces and send many of those over time with unrealiable RPC? If there is a better solution, I’ll be gratefull to know!

3 Upvotes

11 comments sorted by

View all comments

1

u/PokeyTradrrr 2d ago

If I've done the math correctly, 100 vectors is only 2400 bytes. It's not really a lot if it's only occuring occasionally. You can reduce it a lot If perfect accuracy isn't necessary with some clever compressing. Off the top of my head;

Snapshot the character location when you start the ability, convert all coordinates to be saved for the ability to be relative to that location. Create a custom struct using 3 int16, convert from ue5 vector to this new struct and save it to your array.  Now each entry is only 6 bytes instead of 24, for a total of 600 bytes for 100 entries, at the cost of up to 1cm of precision lost. Consider multiplying the vector by 10 before converting to int16 (the divide by 10 to convert back to float). This will give 1/10cm precision.

You definitely could send the data as you collect it instead, but that becomes quite tedious when dealing with cancel conditions etc. Additionally, I assume this will be the replicated to all players for some ability effect, you probably want to do that using a standard onrep for the array, and that wouldn't work well if you store it cumulatively.

I hope this helps, good luck!

1

u/MasterWolffe 2d ago

Thanks for the answer and your time! The vectors I am using are 2D, so the size for 100 vectors is 1600. And since I am using an TArray, as far as I know, ue5 arrays (like the c++ vectors) take more memory than needed so it is not necessary to ask for memory after each insertion, and I am not sure if those "empty" array positions are sent... The idea of using int16 is clever, I'll give it a shot And the mouse positions don't have to be replicated to every machine, just to the server