We have Time.get_ticks_msec() which returns time passed in milliseconds since the engine started.
Suppose I pause the game with the default Godot way, this number still increases. This makes sense because time passed in milliseconds since the engine started has nothing to do with the game pausing.
Now, what if I want to take pausing and timescale into account here? Is there a built-in functionality for this? For example, if I pause the game or set the timescale to be not 1, I will have the msec incremental rate changed accordingly.
If such built-in functionality does not exist, a singleton for managing a single variable just for this could be used instead.
But before implementing my own, does such built-in functionality exist?
You can make a simple counter based on the delta in the _process() method. This method is affected by the Engine.time_scale and get_tree().paused (the latter only if the process_mode of the node is set to always, but it’s a default setting).
var counter: float
func _process(delta: float) -> void:
counter += delta * 1000 # delta is in seconds, so multiply by 1000 to get miliseconds.
See this little demo, where I change Engine.time_scale and get_tree().paused and you can see how it affects both the delta timer and the Time.get_ticks_msec().
You can also use _physics_process as that ticks 60 times a second by default. I believe it’s a little more consistent than process if you don’t plan to change the physics tick rate.
var counter: float = 0.0
func _physics_process(delta: float) -> void:
counter += delta
# Will tick based on physics ticks in project settings.
#(Defaults to 60 a second)
From here, I can conclude that there are 2 possibilities:
the _process on a singleton will be the way to go in this case.
However, the _physics_process is another possibility, if your game requires that no frames are to be skipped (ex. physics processing or you are doing bullet hells or some sort of a game that it is preferable for the game to slow down with respect to the framerate rather than skipping frames)
Either way, this will take pausing and timescale into account now.