I’m making a cooking game and when the user presses space it interacts with the object on the counter. I scripted it so when I press space, it gets a range around the player for all the counters and FireServer the closest station. The server then receives this request and checks to ensure the station they want to use is within range (prevent exploits).
My question is, there appears to be a delay when the user presses space, resulting in a bit of a delay from the client to the server. Am I doing the most efficient way, or is there a better way to accomplish this to reduce delay?
Do everything on the client without asking the server for immediate results, and at the same time ping the server to let it know what happened. If the request was valid, replicate to all other clients. If the request was invalid, ignore it – you don’t have to do anything further since the change on the exploiter’s client doesn’t affect gameplay.
It will look awful if you do it on the server, since the rate that the network updates at is significantly lower than the rate that the client renders at. That means that even if you have a perfect connection the animation won’t look very smooth because the client won’t get an update every frame.
You might want to tell the client “that was not valid” instead of just “ignor[ing] it”. Perhaps that was meant to be implied, but I don’t think it’s clear to someone implementing client-server networking for the first time.
Small differences between the client and server can result in regular clients sending invalid input sometimes. For example, a player that is walking towards an interact-able object might be close enough on the client, but too far on the server. It would be bad for a regular client to get desynced from the server like that.
If the client receives a “that was not valid” response, it should undo its local changes. This is better than getting desynced.
You’re probably going to want the clients to make the change, or else if you make it on the server, the original client will get the update as well – users will see any change they make twice. Manually replicating to clients also allows you to optimize what’s sent over the network. Instead of replicating a whole room when it loads, you can just send the IDs and positions of the items in the room, and the receiving clients can construct the room with known info from there.