So I’ve been making a (two player) game for a while, and I want to add an online mode. I’m able to get the Server, Client, etc. classes working the way I want, so that’s fine. I’m just wondering how I would go about making the actual game playable over the internet. I’ve decided that one player will have a server as well as a client, and the second player will have just the client. These are the two strategies I thought of:
Have the client-only player send their control inputs to the server player. The server player sends them back a picture of the screen, and the sounds they should be playing. (both players have the same camera view in the offline mode, btw)
PROS: Simple to implement, and if I ever want to add more than two players it will be easy to do.
CONS: Essentially streaming a video to the client, which seems like it would be pretty slow. Sure, the client has all the audio assets, and the screen resolution is only 640x480 (it’s a retro-style game), but it’s still video streaming.
Player 1 sends their inputs to player 2, and vice versa. Each player process the inputs and runs the game on their own.
PROS: This is the more “normal” way of doing things, and would likely be much faster than the other method.
CONS: There’s more of a risk of gameplay becoming… “desynced”? For lack of a better word. If any of the inputs get dropped, or if I forget to keep the same seed for any of my random number generators, gameplay between the two players will end up differently. This can be prevented, but it will be much more work.
What do you all advise? Some variation of these? A third method?
(I’ve been trying to find tutorials for this, but my Google-Fu must not be that great. So, sorry if this has already been answered)
The first one is really a bad idea. The second one is fine, but it depends on what game you are doing, if you have some sort of result from these inputs you can just send it.
For example on an fps game you would send player positions, not key inputs.
About the synchronization: multiplayer games are always out of sync, the goal is to make the player believe they aren’t, but how you do that depends again on the kind of game you are making.
I could send the inputs to the server, then have the server send back things like the position of all entities, and the current frame of the current animation (e.g. frame 2 of punch1 animation)? I’d probably have to modify or extend the Picture class so that it remembers the source of its images, but I guess that could work?
If so i don’t think SimEthereal would fit. The usual approach for these games is to send input keys, use authoritative clients, sync the actions by adding artificial latency and hide glitches with animations.
You can for example send a fixed number of packets per seconds that carry the timestamp and one or none input, when there is one missing, you pause the simulation and wait.
Authoritative clients- meaning, a server hosted somewhere, and both players are only clients?
No, well it can be also two clients and no server, it doesn’t really matter.
To make an example, authoritative server is when your client say “I throw a punch” and the server reply “You hit something”, authoritative client is when your client say “I throw a punch and hit something” and the server/other client believe you.
With authoritative clients you basically trust the player that may use cheats, but you avoid some latency.
Then off course you can mix both ways.
Of course, nothing’s stopping me from pausing the simulation on both
ends when a packet is missing regardless. So this is still helpful information.
Yes, you have to do this in any case, since there is always latency in both ends, no matter what.