Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at the process and physics process for trial methods. Now Godot offers us two ways of dealing with frame rate and dependency.
And that is through the process virtual method and the physics process. Virtual method. Notice how both of these methods have an argument. However they do not require you to return back of value and as a quick refresher the Delta parameter is in fact a float value. Let's start with the process virtual method. Again the process virtual method gives us the ability to be frame independent or excuse me frame rate independent if overridden by node. It will run at every frame possible.
This means that the process virtual method prefers speed over consistency and as a matter of fact the process virtual method is run with the game loop or in this case the main loop since we're dealing with could do. Let's go ahead and look at some output. Now in this formula that we're outputting to scream we're outputting time which will be here and it's compounding that means we keep adding a value to it. In this case.
The value is the delta time and we are starting at zero. So notice as you can see here are process step on every frame adds to time which we output to screen time being the total time. Notice how every time we keep adding a value. But in this example I want to show you what the value is at the first second. As you can see here we get to basically frame fifty six. Notice how we are at one second. However we're not at the perfect one second. We are at something like 1 points or 1.
As you can see here are delta value or Delta float.
Value is not consistent. It's not a constant value that's being passed to us. And so that's one thing to keep in mind with the process virtual method. Moving on. When do we use the process virtual method. Well for one thing if you need an action code or something updated at the fastest possible rate you'll have that logic your code logic inside of the process or method.
An example would be real time gaming or multiplayer.
Now another reason you may want to use the process virtual method is when you want these smooth this movement possible that the physics process virtual method is unable to output for you. Moving on.
So let's go ahead and look at the process virtual methods and the negatives. Now the negatives of using the delta value in the process virtual method is that the delta value is not reliable and that's OK by reliable I mean sometimes it will give you back a zero value. Now the reason for this is in a sense just how Godot handles code at the lower level. However as a summary especially if you're a beginner and just know that the process virtual method prefers speed over flow value consistency.
Now the default settings for any new project and Godot has an option called v sync on and this is a good project setting to have because most projects won't even need the powerhouse that the process for trial method provides. However there are times where you may want to consider having the process virtual method call as many times as possible and turning off the v sync option is one possible option. Now that means that by default you have the three sync setting on and when you do have this on.
There are times where the process virtual method runs slower than the physics process virtual method and now why exactly is that. Well the reason for that is that because in the lower levels of the code Godot is trying to help you out by limiting how many times the process method is actually called as a matter of fact the process virtual method in a sense is not sent to the hardware as a matter of fact. That's because the process virtual method every time we call we are not sent to the hardware frame for example the hardware being the laptop or computer you have. It
It has a monitor that monitor has a refresh rate. The process virtual method does not care about sinking itself onto the hardware that the game is currently running on it gets a little deep. But the short version summary is that process virtual method primarily only focuses on the speed at which it gets caught. Now if you want to see for yourself how fast the process virtual method can get called the easiest way is by removing the v sync option. So let's go ahead and take a look at how to get our process virtual method as best as possible or rather get caught as fast as possible.
So first you want to remove the sync from the project settings as a matter of fact I recommend that you never ever ever do this unless you know for a fact that you need the process virtual method to get caught at the at the fastest possible rate. Now when you go to project settings you're going to see tabs under general going to see a small search box and it will be to the left directly underneath the general tip. You're gonna go ahead and click here you're going to type in B sync. You're going to see on the left side something called display and one of the options will be window and you're gonna find one of the settings under the display window.
Option settings is v sync and by default it has the checkbox marked on. Now what you want to do is you want to uncheck this box. It's gonna give you the option to save and restart at the bottom. You go ahead and click that and then voila. Your game does not run with the v sync setting on. This means that the Godot application will not limit the process virtual method. As a matter of fact your process virtual method will get called as fast as the CPO allows it to get cold. Now this is the example with the v sync on it's the same exact thing as the previous slide for the virtual process method and it's the same thing time plus equals delta.
So you can see here are process step starts at 1. And look we only hit fifty six frames in quotations frames or rather our process step gets called only 56 times in order to hit one second. And as a matter of fact that means that the process step or rather the process virtual method gets called less times than the physics process virtual method. And that's because the physics process virtual method guarantees to be called at least 60 times a second and we'll look at the physics process virtual method later but for now the process virtual method in this case in this instance it gets called less than the physics process virtual method and as a matter of fact with the v sync on it's not guaranteed that you're going to for example in this case hit one second every fifty seven frames in some instances it might be 90 frames others it could be sixty three and this is what I mean by the fact that the process virtual method is not reliable we do not know how many times the process virtual method will get called every second.
Now let's look at an example with the process virtual method with a v sync turned off. Now we have the same thing time plus equals delta we're compounding. But because we turned v sync off that means in this case our process virtual method will be called as fast as the CPO allows it in this case. So as you can see here what you're going to see is that in this example I was able to clip process steps 10 through 15. And look how our value doesn't change. Now this means that we're calling the process virtual method in between our hardware frames.
That means in this case we got called six times in between frame one and frame two. And this is what I mean by being called as fast as possible. Now as a matter of fact this sometimes happens with v sync turned on however it is very rare to see values that are hitting on what I like to call the idle frame the idle frame being the time in between the hardware frame one and the hardware frame too. Now as a matter of fact if you can see here in order to reach one second it took our process step three thousand seven hundred ninety one times before the total time of one second has elapsed.
Compare that to our process virtual method with v sync on which was only called 50 times or rather 57 times per second. Now in most cases you do not want v sync off you most likely do not want whatever cold logic is inside your process virtual method to be called three thousand seven hundred ninety one times. However if you really really want real time this is one option. However again do not turn v sync off this is just educational purpose to let you know that the process virtual method has the capability of running at least in this case for my specific computer.
Three thousand seven hundred ninety one times. Again this is probably something you do not want if your code is called Four thousand times a second. This is where performance issues come into play.
Let's move on now the second method we are going to look at is the physics process virtual method. Again this virtual method gives us the ability to be frame rate independent. Now if the physics process virtual method is overridden by a node Delta will be capped at one sixtieth of a second. And this is the default value. And what that basically means is that we will run this script 60 times a second. And basically we will get a value of zero point one seconds per six frames as a matter of fact this delta value is constant.
And one thing to keep in mind is that the physics process virtual method is sync to the physics processing in our main loop. Let's go ahead and take a look at the physics process. Virtual method. Now as you can see here we have the same thing. We have a time and we are compounding the value of delta being given to us. We are our time value is starting at zero. As you can see here we're given basically a constant value. And notice here at physics Step Six our value is zero point one. Just like in the previous slide the delta time has kept that one 6 year we can take a look over here.
If we move on. So over here you can notice that at the Fifty fourth frame or values zero point nine. That means every six frames we were given the value zero point one and the sixtieth frame we are at one end because we're compounding time we would say that at one second has elapsed. And in this case the physics step is giving us a perfect value of one. So when exactly do we want to use the physics process virtual method. Well for one when we're dealing with physics such as movement and b physics process virtual method is good when we want to create custom timers. And
And that's because we're being given back the value one every sixtieth frame. And so in a sense it's easier when we want to create our own custom timer's and by custom timer's I mean hard coding some time or logic not using the timer node. And it would be perfect to have then the physics process virtual method because we get back the whole value for every 60 frames so we could count one two three four five six for every sixtieth frame we hit. And lastly the physics process virtual method is ideal for Game Logic for example path finding or updating positions for your path finding that could be as simple as giving the enemy the vector value of your player position and having your enemy move towards the player is preferable to have code logic instead of physics presses for performance reasons.
Now there are negatives to using the physics process virtual method and for one or Delta constant is kept the one over 16. Basically if you need to update something at the fastest possible rate you may want to consider having that code logic instead of the process for your method. Now again the physics process virtual method may not offer you the smoothness visual animations. It could vary. The mileage can vary. However if you feel that animation is not at its smoothest when you're cold logic is and is inside the physics process virtual method you may want to consider moving that to the process.
Virtual method I'm going to give my opinionated preference. The one thing that is not an opinionated preference is that anything that deals with physics needs to be inside the physics process. Virtual method. Now to reiterate the reason why we want our logic for physics calculations such as movement inside the physics process is because of the value deltas being given to us which is a constant. However for cold logic that is outside of the physics realm. Everything I am about to explain is preference or rather personal preference.
So if you are a beginner the best thing to do is to put all your code logic inside physics process virtual method. Now if something feels weird and I feel weird I mean for example maybe you want an item to rotate by your rotation is not inherently important for the game. And so the way it moves doesn't have to be perfectly calculated for example perhaps this is some type of rotation in the background that has nothing to do with game mechanics. Well if you had that rotation logic inside a physics process virtual method and you felt that it was slow or sluggish or just didn't feel right then go ahead and take that cold logic.
In this case the rotation cold logic which has no value to your game again and it is not something dealing with physics such as movement then go ahead and move that into the process. Virtual method. Okay. So now you've got this process right where you put everything in physics process and then for some things you want to put it into process and this is a good way to start. Especially as a beginner. However as your game grows big your game will get slow especially if your cold logic is inside the process virtual method.
So when your game is slow the first place to look well the real first place to look is the debugger and check if you have a memory leak. We'll talk about that in a future future episode. However if there is no memory leak then the next best place to look is inside the process. Virtual method. Go ahead and look at the cold logic that you have in your game that is inside process. Virtual method and see if you can go ahead and move that back into a physics process. Virtual method. And this is a good pattern to get into. If
If you are a beginner one last thing I want to point out is that I do have a project on get hope. I have two scripts that touch one is seeing the time compound with the delta time and being output it onto the console and so that's script Number one it's attached to the root node so feel free to check that out. I also have a second script attached to a sprite that does rotation so you can see for yourself if the rotation looks weird when it's run in the physics process virtual method versus the process virtual method. So go ahead and check that out player around see what you like and what you don't like about both of those methods.
Get a feel for it. And yeah that's basically it. As a matter of fact most of your game logic and your movement and physics all that code will be put inside either the process virtual method or the physics process. Virtual method so it isn't portent to understand how both virtual methods work their benefits. Why cold logic is preferred to be put under one method versus the other is a matter of fact you may find that for most games it really doesn't matter where you put your cold logic. However as your game grows it gets bigger. A performance becomes a concern and it is when performance becomes a concern that having a better understanding of both virtual methods will help us improve game performance.
Well that's all I have for you in this episode. Thank you so much for joining me. Feel free to leave a comment or question down in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day.