I’m running through the tutorials and I’m seeing the following quite often:
The update() function is called once each frame. The game is running at 60 frames per second so the function is called at an interval of 1/60 seconds. The argument variable dt contains the current frame interval—the number of seconds elapsed since the last call to the function.
I’m not sure if it’s true for all scenarios? i.e. what happens when there’s a very expensive compute. It’ll most likely delay update function right, since lua is single-threaded? And if that’s the case, dt will always be 1/60?
I’ve experience some strange encounters. I’m expecting dt to larger if there’s a delay to the update function. I’m i’m calculating my next position like this: go.get_position() + self.dir * self.speed * dt, which is pretty common. I’ve printed dt, and it seems that it’s always fixed at 1/60, but when the game is running, and after some deliberate expensive computations, there’s a significant drop in the fps, and my character is not moving slower and slower. I’m expecting the character to move the same speed since * dt should take care of it, the only difference should be just a drop in fps.
Yes, you can delay calls to update by doing computations. The value of dt will stick to 1/60 (or whatever framerate you have selected) though - unless you set ”variable dt” in the project settings.
Love your videos & tutorials, there were very informative. Thanks for the answer, might be good if there’s some hints snuck in, maybe in the tutorials or the update function as comments?
" A coroutine is similar to a thread (in the sense of multithreading): a line of execution, with its own stack, its own local variables, and its own instruction pointer; but sharing global variables and mostly anything else with other coroutines. The main difference between threads and coroutines is that, conceptually (or literally, in a multiprocessor machine), a program with threads runs several threads concurrently. Coroutines, on the other hand, are collaborative: A program with coroutines is, at any given time, running only one of its coroutines and this running coroutine only suspends its execution when it explicitly requests to be suspended."