Networked Game, Injecting Updates, Opinions wanted.

Posted 14 October 2013 - 11:27 AM

I'm still in the planning phase of a lot of my systems/concepts, so I don't have any actual code to show you guys, more-so a concept I'm planning to use to "inject" updates into my game, and was wondering if something like this would work as expected, if there would be a better way to go about it, or any possible flaws about the system I have in mind.

Basically the networking components and the actual game will be seperate systems. They will both be running concurrently but they will be unaware of each other. The thing is that the networking system will be receiving game state updates from the server, and these updates will need to be applied into various area's of the game(such as game objects, other players movements, messages in chat, etc.)

This is a basic outline of the system I have in mind....

Breakdown-

The networking component just listens for data, when it receives data it passes it to a different networking class responsible for processing the byte data, it then returns to listen for more data.

The component responsible for processing the data will also be responsible for passing any relevant updates into a sort of "mediator" class, the Update Manager. The "Update Manager" gets called once every x seconds in the game's update cycle(still deciding on how often to "inject" updates). I plan to use synchronization here, in the form of locks. I plan to lock the data structures in the "Update Manager" when it begins its update cycle in the game loop, so the networking side of the game isn't corrupting data during the process(such as passing in new data while i'm iterating over it).

Do you guys see any potential pitfalls with this system? Do you think there would be a better way to do this? Any flaws with what I have in mind?

Replies To: Networked Game, Injecting Updates, Opinions wanted.

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 14 October 2013 - 11:57 AM

If you are making a real-time online game, I'd suggest just processing and updating the game objects as they are sent out by the server instead of once every X seconds. This will mean that you have a bit more to do each loop, but will also reduce the possibility of having a TON of stuff to do sometimes and barely anything others. So, I'd suggest doing the work as it comes in, that'll make the game move "living".

I'd also suggest a process flow closer to this (NOTE - there are only a few differences):

Hopefully there you can tell that we have 3 (minimum) separate threads. The first being the game loop, the second being the update manager (which I am assuming will also be doing other things, such as user input (keyboard, mouse, etc.)), and the final one being the networking. When the networking thread gets data it processes it and passes it over to the update manager then listens for more data. The update manager waits for the update from the network (or keyboard, etc.) and just stores the information. The game updates and polls the update manager for any updates, then draws them.

Do note that this method makes all calls to the update manager blocking, so if the network is writing data to the update manager and the game loop wants to poll the update manager, the game loop will have to wait until the network write is done, and same for the other direction. So it may make some sense to have a shared data structure between the network and update manager which is write only for the network and read only for the updater in such a way that a read and write can be happening at the same time (such as a linked list that locks to get the head and tail of the list then unlocks -- quick 2 functions) and just loops through the known head and tail then deletes all of the read nodes. In the meantime the list can be getting written to from the network and updating the tail->next nodes without a problem (just an example).

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 15 October 2013 - 08:25 AM

modi123_1, on 15 October 2013 - 08:12 AM, said:

Explain what these 'updates' are.. as in new characters, code fixes, or what?

they aren't code updates, content updates, or any of the sort.
They are "game state updates" from the server. For example, if NPC 1 moves left, the server sends out that positional update to all clients on that map. Or, if player1 changes maps, the server sends out all relevant information to that client so it can "load" the map state correctly (such as NPC positions, other players positions, etc). Those kind of updates.

I'm pretty sure I mentioned they were gamestate updates in my original post, but I might have not.

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 15 October 2013 - 09:07 AM

No - you did not mention that.

Sending data to the server, and receiving data back, should be part of your client's normal game loop. I am confused on what benefit it would be to have some sort of whole new thread and then push data out of it and squirt the updates into the game. Why not just have that be a step in the engine?

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 15 October 2013 - 09:41 AM

Well shouldn't the game loop only be worried about updating, rendering the game, and sending data to the server when required(such as when the player moves, sends a message in chat, etc)? I don't see why the game loop would care about when data is received, processing that data, then updating what needs updating, when all it should really be concerned about is applying the updates?

This is why I decided to separate the two systems. The game loop will still be applying the updates every update cycle, but it won't ever need to think about receiving data and processing it. All it needs to worry about is grabbing the updates and applying them.

Re: Networked Game, Injecting Updates, Opinions wanted.

Thank you for this amazing tutorial, but I still want to ask you something, before starting to work with my project.
So, Client should only contain the canvas with the graphics and get inputs from the player? All game logic should be done by the server?(Except that client prediction)

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 15 October 2013 - 11:19 AM

For the most part - yes that is the prevailing theme. Remember - a multiplayer game must not only track NPCs but players well. The only sure way of not bungling the whole thing is to have one "Truth" of that from the game server.. Sure the clients can do some predictive movement and actions, but to know 'exactly' where everyone is synced you need only one place to look.

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 15 October 2013 - 11:33 AM

modi123_1, on 15 October 2013 - 11:19 AM, said:

For the most part - yes that is the prevailing theme. Remember - a multiplayer game must not only track NPCs but players well. The only sure way of not bungling the whole thing is to have one "Truth" of that from the game server.. Sure the clients can do some predictive movement and actions, but to know 'exactly' where everyone is synced you need only one place to look.

I guess, even if client predicts the players movement, the original state should be set by the server yes? How about the other player predictions? I mean how can u predict the things you do not actually see. (What button the other player will press).

Re: Networked Game, Injecting Updates, Opinions wanted.

Posted 15 October 2013 - 11:54 AM

I think that is addressed in that other thread I posted, but you can tune your game to make educated guesses about basic things. If once player is going at a specific speed and direction you can make some assumptions that will continue for the short term.. etc.