-
Notifications
You must be signed in to change notification settings - Fork 143
BYTEPATH #1 - Game Loop #15
Comments
What's the difference between delta time and a fixed delta time? |
@hikkithegene Delta time is a concept, fixed delta time is an implementation of the concept. |
The Fix Your Timestep article seems to have been removed. However, I think this is the same article: https://github.com/gafferongames/gafferongames/blob/master/content/post/fix_your_timestep.md so maybe change the link to that? |
Sorry for my english @doble-d Delta time is the last time the function was executed minus the function at that exact moment, and this value can vary depending on the machine, so the fixed delta time is like a fixed value so that the delta time does not vary, making the game stay concise. An example is when you hold the LOVE window, when you hold the game freezes, and while it is frozen it accumulates delta time, when you release the window it uses that accumulated value in the calculation, so it can break your game, like a animation go much more to the side than expected. With this, the fixed delta time is used as a limiter, so that when the game freezes it is no more than a basic value, which will always be the highest value that the machine can make, so when the player releases the window the game returns as if nothing had happened. You can thing in something like that the delta time: if dt > 0.30 then return end
-- other functions But in reality, it is much more detailed or fixed, but it is a simple and very basic example. But that's what I think, I came from unity3d, and that's what I learned from it and things. If I am wrong someone please tell me, I know the Löve is really different from unity So that is it. Hope I help you in some way. ❤️ |
I'm a bit confused about the variable delta time exercise. It pretty much just grabs the time between two frames and uses that as dt. Am I missing something here? |
Start
To start off you need to install LÖVE on your system and then figure out how to run LÖVE projects. The LÖVE version we'll be using is 0.10.2 and it can be downloaded here. If you're in the future and a new version of LÖVE has been released you can get 0.10.2 here. You can follow the steps from this page for further details. Once that's done you should create a
main.lua
file in your project folder with the following contents:If you run this you should see a window popup and it should show a black screen. In the code above, once your LÖVE project is run the
love.load
function is run once at the start of the program andlove.update
andlove.draw
are run every frame. So, for instance, if you wanted to load an image and draw it, you'd do something like this:love.graphics.newImage
loads the image texture to theimage
variable and then every frame it's drawn at position 0, 0. To see thatlove.draw
actually draws the image on every frame, try this:The default size of the window is
800x600
, so what this should do is randomly draw the image around the screen really fast:Note that between every frame the screen is cleared, otherwise the image you're drawing randomly would slowly fill the entire screen as it is drawn in random positions. This happens because LÖVE provides a default game loop for its projects that clears the screen at the end of every frame. I'll go over this game loop and how you can change it now.
Game Loop
The default game loop LÖVE uses can be found in the
love.run
page, and it looks like this:When the program starts
love.run
is run and then from there everything happens. The function is fairly well commented and you can find out what each function does on the LÖVE wiki. But I'll go over the basics:In the first line we're checking to see if
love.math
is not nil. In Lua all values are true, except for false and nil, so theif love.math
condition will be true iflove.math
is defined as anything at all. In the case of LÖVE these variables are set to be enabled or not in theconf.lua
file. You don't need to worry about this file for now, but I'm just mentioning it because it's in that file that you can enable or disable individual systems likelove.math
, and so that's why there's a check to see if it's enabled or not before anything is done with one of its functions.In general, if a variable is not defined in Lua and you refer to it in any way, it will return a nil value. So if you ask
if random_variable
then this will be false unless you defined it before, likerandom_variable = 1
.In any case, if the
love.math
module is enabled (which it is by default) then its seed is set based on the current time. Seelove.math.setRandomSeed
andos.time
. After doing this, thelove.load
function is called:arg
are the command line arguments passed to the LÖVE executable when it runs the project. And as you can see, the reason whylove.load
only runs once is because it's only called once, while the update and draw functions are called multiple times inside a loop (and each iteration of that loop corresponds to a frame).After calling
love.load
and after that function does all its work, we verify thatlove.timer
is defined and calllove.timer.step
, which measures the time taken between the two last frames. As the comment explains,love.load
might take a long time to process (because it might load all sorts of things like images and sounds) and that time shouldn't be the first thing returned bylove.timer.getDelta
on the first frame of the game.dt
is also initialized to 0 here. Variables in Lua are global by default, so by sayinglocal dt
it's being defined only to the local scope of the current block, which in this case is thelove.run
function. See more on blocks here.This is where the main loop starts. The first thing that is done on each frame is the processing of events.
love.event.pump
pushes events to the event queue and according to its description those events are generated by the user in some way, so think key presses, mouse clicks, window resizes, window focus lost/gained and stuff like that. The loop usinglove.event.poll
goes over the event queue and handles each event.love.handlers
is a table of functions that calls the relevant callbacks. So, for instance,love.handlers.quit
will call thelove.quit
function if it exists.One of the things about LÖVE is that you can define callbacks in the
main.lua
file that will get called when an event happens. A full list of all callbacks is available here. I'll go over callbacks in more detail later, but this is how all that happens. Thea, b, c, d, e, f
arguments you can see passed tolove.handlers[name]
are all the possible arguments that can be used by the relevant functions. For instance,love.keypressed
receives as arguments the key pressed, its scancode and if the key press event is a repeat. So in the case oflove.keypressed
thea, b, c
values would be defined as something whiled, e, f
would be nil.love.timer.step
measures the time between the two last frames and changes the value returned bylove.timer.getDelta
. So in this casedt
will contain the time taken for the last frame to run. This is useful because then this value is passed to thelove.update
function, and from there it can be used in the game to define things with constant speeds, despite frame rate changes.After calling
love.update
,love.draw
is called. But before that we verify that thelove.graphics
module exists and that we can draw to the screen vialove.graphics.isActive
. The screen is cleared to the defined background color (initially black) vialove.graphics.clear
, transformations are reset vialove.graphics.origin
,love.draw
is finally called and thenlove.graphics.present
is used to push everything drawn inlove.draw
to the screen. And then finally:I never understood why
love.timer.sleep
needs to be here at the end of the frame, but the explanation given by a LÖVE developer here seems reasonable enough.And with that the
love.run
function ends. Everything that happens inside thewhile true
loop is referred to as a frame, which means thatlove.update
andlove.draw
are called once per frame. The entire game is basically repeating the contents of that loop really fast (like at 60 frames per second), so get used to that idea. I remember when I was starting it took me a while to get an instinctive handle on how this worked for some reason.There's a helpful discussion on this function on the LÖVE forums if you want to read more about it.
Anyway, if you don't want to you don't need to understand all of this at the start, but it's helpful to be somewhat comfortable with editing how your game loop works and to figure out how you want it to work exactly. There's an excellent article that goes over different game loop techniques and does a good job of explaining each. You can find it here.
Game Loop Exercises
1. What is the role that Vsync plays in the game loop? It is enabled by default and you can disable it by calling
love.window.setMode
with thevsync
attribute set to false.2. Implement the
Fixed Delta Time
loop from the Fix Your Timestep article by changinglove.run
.3. Implement the
Variable Delta Time
loop from the Fix Your Timestep article by changinglove.run
.4. Implement the
Semi-Fixed Timestep
loop from the Fix Your Timestep article by changinglove.run
.5. Implement the
Free the Physics
loop from the Fix Your Timestep article by changinglove.run
.BYTEPATH on Steam
Tutorial files
The text was updated successfully, but these errors were encountered: