Hello and welcome to another episode in the Godot basics tutorial series. In this episode I will be taking a look at the game loop. If you are a game programmer or have experience in programming. This episode will most likely be redundant. However if you are new to programming basically you're starting from zero. Then this episode may be worth taking a look at. So let's go ahead. At its core all games can be understood as having one function that contains all of the game logic logic being written code.
The cycle of running this function repeatedly is called a game loop. Everything on your computer screen is just a static image that updates itself repeatedly. And this gives us the illusion of movement.
As a matter of fact think of computers like watching a movie on television or your being presented as still images at a constant rate.
In movies it's most likely 24 frames per second.
So basically when you're watching a movie every second you're being shown 24 different pictures every second.
And as it does this constantly you get the illusion of movement through still images and computers are exactly like that. This is an important concept to understand if you're new to programming because you understand how many times your code runs per second. So let's take a look at that. Let's say you have a computer with hardware and it maxes out at 60 frames per second. Well your code runs 60 times per second. It's a matter of fact that's all game programming if game programming is running code depending on how you program.
But essentially you'll most likely be running code every frame for as many times as your computer can handle. And so if your computer can run 60 frames per second you will most likely be running code 60 times per second. Although this is not always guaranteed.
Now one other thing I just wanted to throw out is that you are limited to your monitor.
So if your monitor can only handle 30 frames per second the user will only see 30 frames per second over since your computer can handle 60 frames per second. You will be running 60 times per second so that's another interesting thing to keep in mind.
Now this isn't always the case.
Some use of firm G sinking or V sinking back sinks your computer to your monitors for example stead of 60 frames per second you would drop to 30 frames per second and therefore they load off your GP you is reduced in half in this case.
Our point is when it comes to the game loop you are running based on your frames per second.
And good though JD script if you're using underscore physics process you'll be maxing out at 60 frames per second.
With underscore process you'll be running your code at its max limit so whatever your computer can handle so just something to keep in mind your code runs basically multiple times a second. Moving on let's take a look at the game loop pseudocode. So basically our game loop no matter what game engine you choose is just basically one big while loop. Basically it only checks two things One you have not quit the game or two. You have not crashed and now the different game engines have different ways of handling the game loop.
They all essentially do the same thing.
First they update physics and they update your scene.
The images you're shown this is where pictures come into play from the movie example. It basically races the screen and then redraws back to the screen. That's
That's how you get your illusion of movement. Then if you receive input from the player for example moving the mouse or clicking on the keyboard you get past those inputs so you can manipulate character move men or do some action based on what the user has pressed or click. And after that we have performance updates each game engine does its own performance updates. This is the beauty of game engines we don't have to handle the performance updates. However just know that most game engines will handle performance for you.
And then last but not least we're basically checking if our user has still not quit and or our game has not crashed.
And if it hasn't then we just repeat again. And that's basically it. That's basically a simple game. And keep in mind you run this for every frame. Now some things we don't have to worry about if we're using good those built in physics features good deal we'll update our physics for us Cardoso updates are seen however we can also write code to update our scene for example moving a character. We
We also have code that can take in player input so we have control over receiving player inputs as well. We do not have control over performance that is handled by the game engine.
Basically it's more complicated things that's outside of the scope of this series such as memory management and culling.
And then basically we just run all of this repeatedly until our player quits or the game crashes.
Now why exactly is understanding this important. Well one thing is that we have to be mindful of how we code for example a big while loop could basically cause our game to crash a big for loop could also make our game crash or be really really sluggish. Same thing with doing complex mathematical algorithms and calculations that can also make our game sluggish or crash as well.
Basically we have to be mindful of how we write code because our code will be run multiple times per second and that basically leads me to the next point which is because we have to be mindful of how we write code. We need a good understanding of the script life cycles.
Basically we need to know where certain lines of code should be placed in our classes and so we will be taking a look at that.
And then next step aside script life cycles ever if you're more experienced and you're interested in learning more about how Gordo handles everything.
Please feel free to take a look at the good deal get up page for the main loop. You'll be wanting to take a look at the main loop C++ file and you'll find the iteration function towards the bottom and that's where we run our game loop. Another area I recommend to look at is the main dot C++ file towards the bottom.
You're going to look for the iteration function as well and the main that C++ is the highest level function that runs not only our game but also our good tool Ed..
So keep that in mind.
Thank you so much for joining me. I hope you learned a lot if you're new to programming.
If you have any questions or comments please feel free to leave them in the comments section down below.
I look forward to seeing you in the next episode. Have an amazing day.