- Home /
Networking noob; how do I sync / time input with RPCs?
I already have it so the first player to start the game registers with the MasterServer as the host and the second player finds the game and connects as a client, and then when either player hits "play," both instances of the game start at the same time. I guess my question is "Now what?" so I'll try to give my own answer to that and see if someone else has a better one...
The game pieces move in discrete units every game "tick". (~ 0.5sec) The player can press keys (or onscreen buttons) to choose a direction at any time in the cycle, but when the 'metronome' function invokes they move in whatever direction the most recent input said to, or forward if there was no input that time. This seems like it should be one of the easiest control styles to make multiplayer, but it is my first such effort.
My main problem is what to do right at the end of the timer period when a different key is pressed within the client's time limit but lag makes the call get there after the server has already recorded the previous keypress as the 'final' one.
Also not sure what to send with the RPC (could it just be one byte?) and when to send it. My idea was that locally there would be a lastKey variable that updates immediately each time there was an input. Then two RPC functions that just ping-pong back and forth constantly regardless whether lastKey has changed (player 1 RPCs a function on player 2 that first updates the theirKey variable on player 2's game to player 1's lastKey and saves the timeStamp, then player 2 RPCs the same function on player 1. So each player's theirKey is no older than the latency time from lastKey, but no duplicate messages are sent.
Is that all wrong? Am I missing something important? It seems like there are just a lot more ways to do something that are wrong when it comes to multiplayer communication.
( I did read the docs and whatever else I could find to read but it feels like the Unity team wants people to figure out how to use the networking on their own.)
Answer by raoz · Dec 22, 2012 at 10:30 PM
If you do care about security, than this is the way to go, however, if you don't care about security, than you could have the input calculated on the client and the positions being sent back and forward. The problem with your solution is that it might get out of sync on the 2 ends.
Your answer
Follow this Question
Related Questions
RPC or Synchronization ? 1 Answer
How to send timestamp via RPC so it was relative to the receiver? 1 Answer
How to measure network packet delay? 0 Answers
Countdown issue with Networking 0 Answers
RPCMode.All Question 1 Answer