Game Loop Organization

I am making my first game and I have realised that I don’t know how I should structure my game loop.

Currently it’s done this way:

I have 2 controllers (or state machines, call however you want) on the server – 1 for lobby and 1 for game. They have some states inside. I have controllers with same names and states on client.

Controllers on server send to particular clients through remotes signals to change state.

With this simple concept I immediately got some problems:

  • Let’s imagine we are currently at state where timer on client ticks and updates how much time is left for the finish of the voting. Now, when player enters the game, he can’t simply start his timer – he needs a timestamp of start of the timer for timers to be synced. So for that reason I am not just sending signal to client, but I also started to send some additional information.

Then I got another problem:

  • My game controller has state “Hiding” and state “Round” and some other states. On “Hiding” I call some functions, enable some UI and so on. On “Round” I also do it. And I allow players to join the round if they were not in it. Now if player joins in the moment of “Hiding” state I need to call some functions in his client controller from previous states (enable some UI that relates to all game, enable some scripts that are also related to all game, fire some signals and so on). With the same time some of functions I don’t need to call (enable some temporary UI that was related only to states before and so on). But if player joins in the moment of “Round” state, he needs to also do all this things, but he also needs to call all required functions from state “Hiding” and this chain goes on.

It becomes a mess and I start to realize that I don’t know how to structure all of that. Maybe I don’t even need controllers and there are easier ways? Please, share some of your experience.

1 Like

The way I like to organize this sort of thing is, I make a folder called “Systems” for both server and client, and inside of that I just put a bunch of a module scripts.

Each of those module scripts has a .Initialize() function, and I just have a script go through all the modules in the folder and call that when the game starts / a player joins.

This is useful in my opinion, because then you can have a specific module called “RoundHandler”. When initialized, you could define all the variables necessary, start any connections, etc. It also lets me easily iterate upon what should happen on initialization, since I can easily just add or remove systems.

Since these modules are started with .Initialize() and not just requiring, it lets other systems require those same modules and use their methods without causing initialization. Such as, if I had a module that handles if a player is sprinting or not, and another module wanted to check that value, I can easily do so.

I’m not really sure if this was the sort of help you were looking for, but this is usually the structure I take when making anything. Let me know if this was helpful!