I have a fundamental #GameDev question that has been bugging me for ages.

I've tried developing my own games, but I always felt like I was missing something fundamental about #programming logic and everything I did felt "wrong" and inefficient.

Here goes.

At the most basic level, games run as endless loops, right? Every frame, something happens. The program goes through the same loop over and over again until something causes it to exit.

For example, in a simple Asteroids-like shoot-em-up, the game goes through the list of obstacles every frame and sets their position downward a bit. They therefore appear to be coming towards you over time.

But games are obviously complex and interactive, and therefore don't just do the same thing every frame. The game needs different things to happen each iteration of the loop, based on what's going on in the game.

For example, the Asteroids obstacle needs to be removed from the game once it exits the screen, or once it overlaps with the player.

The only way I know to check whether something happened is to add a conditional to the game loop. Every single frame, the game checks for every single obstacle whether its position is outside the boundaries, and if so, removes it.

A computer can't just "keep in mind" to watch for a certain event to make something happen, it needs to explicitly be reminded to check every frame.

Even if something is supposed to happen only every minute, you still need to count a number up every frame until you reach a minute's equivalent amount of frames, and then the counter gets reset to zero and the event happens.

I understand all this works.

But what I don't understand is the following:
This seems extremely wasteful and inefficient, right?

I understand that a computer can calculate and compare a bazillion things a second, and that this is perfectly fine for something as simple as Asteroids.

But what about more complex games?

  • Do Mario jump-and-run games genuinely check whether Mario touches the goal post every single frame of the game?
  • Does GTA V check every single frame whether the player is in a mission, and if so, which one they're in?
  • Do games like #Minecraft check every single frame, for every single animal, whether they're currently on fire?
  • Do games check every single frame whether they're paused, in a menu, or running?

If this is genuinely what's going on, how the hell do complex games even run? When I play Minecraft, does the computer really, 120 times a second, for every single animal in the world, check whether they're currently poisoned? And then also checks whether the game is paused, whether there is mouse input, whether it's raining, whether music is playing, whether a block state has changed, and so on and so on?!

What about something like #BindingOfIsaac? Does it really check every single frame for every one of the thousands of possible items and whether the player has them equipped?

I mean, I'm sure it's abstracted a bit in that the game adds an "event hook" or something whenever you pick up a new item, but underneath all that, surely the game still has to check every frame for every possible hook whether it is active or not?

If I finally understood the answer to this question, I'd understand way more about game development.

@lianna The answers to your questions are generally not a simple yes or no answer, but I will try to give a very broad idea. On a very basic level: Yes, the game will check all the information per frame. If you start to write a simple game, that is a good way to get going and understand what's going on. BUT, in the specifics a lot is going on and it depends on many factors. A few examples:
@lianna A game would try to avoid checking collisions of every object with all others. For that reason, space is divided into distinct regions and other heuristics are used to avoid testing too many pairs. This also extends to something like "did we touch the goal". We could react to a collision with the goal based on the optimizations, no need to check every frame.
@lianna In something like Isaac, you would not check all cominations, but instead have a "bullet" or somilar abstraction that is composed of all current powerups. This structure knows how to fire tears and calculate damage based on its state.
So in general, it is a big mixture of: Test what is needed per frame, try to react to specific things detected, couple state with its own behaviour and also try to avoid computations as much as you can
@lianna Sorry for the multiple posts, but I would suggest: Don't worry too much about it in the beginning! Just follow some tutorials and gain experience, this will lead you do understanding. A bit of programming fundamentals are also helpful, as this will help you judge whether something is inefficient or if there are different ways to organize and operate on data