GETTING STARTED IN GODOT – PART 4 – DELTA

By Alex K.

 

Whenever you play a game, some frames might be slower to process than others.  This might be due to the complexity of the frame’s code, or simply due to something else running on your computer in the background.  Slower frames lead to lag and could even lead to inconsistency in the game.

 

In this short guide, we will look at an easy way to avoid the lag described above.

 

1.     Notice the _process function has a parameter named delta.  It is the amount of time in seconds that has elapsed since the last frame.

 



2.     We can test to see the value of delta by temporarily adding a print statement inside _process.  Note that this can be done even if you already have code in _process.  I simply placed it at the top of the function.

 

 

3.     Run the program.  You should see an output value each frame that is close enough to the decimal value 0.016666666667 which is 1 / 60.  This is because our game is running at a frame rate of 60 frames per second.

Notice though that the outputted value is not always the same.  Sometimes, the value of delta is a little larger or smaller.

 

EXPLANATION OF (delta * 60)

 

Consider the value of (delta * 60).  This value should be 1 all the time if the frames are consistently running at 60 frames per second.  However, if the amount of time between frames was longer than normally, then this value will be more than 1 – usually a small amount over 1. 

When dealing with any movement in a game, if we want to avoid lag, we can multiply the amount of change in the movement by (delta * 60). 

If the elapsed time is 10% greater than expected, then (delta * 60) will be 1.1 which is also 10% greater than 1.  So, the movement amount, after being multiplied by (delta * 60) will also be 10% greater than expected.

 



4.     Let’s consider the _process function code that we wrote last guide and change all of the movement amounts from 10 to the new 10 * (delta * 60).

 

 

5.     Test your program.  It should still work like it did before but it is now lag proof.

6.     You can now delete the print(delta) line.