I’m currently working on a multiplayer game and came upon the question of which way would be best to send the input from the client to the server…
Here are the 3 methods I thought of…
- Always send all input even if it hasn’t changed:
CmdSendInputToServer( vertical, horizontal, sprint, jump );
- Send only changed input, but each one sends its own separate RPC:
if ( vertical != verticalLastSent ) CmdSendVerticalToServer( vertical );
if ( horizontal != horizontalLastSent ) CmdSendHorizontalToServer( horizontal );
if ( sprint != sprintLastSent ) CmdSendSprintToServer( sprint );
if ( jump != jumpLastSent ) CmdSendJumpToServer( jump );
- Package up only the changed input into a struct and send that:
struct Input {
float vertical;
float horizontal;
bool sprint;
bool jump;
}
Input input = new Input();
if ( vertical != verticalLastSent ) input.vertical = vertical;
if ( horizontal != horizontalLastSent ) input.horizontal = horizontal;
if ( sprint != sprintLastSent ) input.sprint = sprint;
if ( jump != jumpLastSent ) input.jump = jump;
CmdSendInputToServer( input );
This one seems like the best of the 3 (I think), but I can’t be sure. Wouldn’t it still be sending the entire struct even if you don’t explicitly set certain values? Plus, I read that sending structs is best avoided whenever possible.
Which of those would be the best on bandwidth, etc? Or is there any difference?
Also, is there another method that I haven’t thought of?
Obviously the above code is nowhere complete, just more of an example to help get the idea across.
I’ve been doing a lot of searching and reading various tutorials, etc but nothing seems to address this specific issue. Most tutorials I’ve seen simply use some variation of the first item in my list above, so it would seem that is the way you’re supposed to do it, but that doesn’t seem very good when it comes to bandwidth. Especially when with input you’re usually sending it pretty much every frame or so.
Sorry if this has been talked about and I just couldn’t find it…
I’m still pretty new to Unet and Unity as a whole, so sorry if this question is kinda “noobish”.
Thanks for any info! ![]()