Godot Version
Godot_v4.2.2-stable_mono_win64
Question
I have a very noobie question. I have this simple for loop:
for n in range(0, 25):
print(n)
How can I wait for a few seconds, let’s say 5, before printing each number in the for loop?
Godot_v4.2.2-stable_mono_win64
I have a very noobie question. I have this simple for loop:
for n in range(0, 25):
print(n)
How can I wait for a few seconds, let’s say 5, before printing each number in the for loop?
Use await get_tree().create_timer(5).timeout
for 5 seconds for example
Thanks for your swift response. I actually thought of doing this before but i forgot to await the timer so it didn’t work.
I awaited it now but the for loop isn’t printing the number iteratibly and in order.
If it might help, i have this for loop in the in-built _process function. That’s where i wanna use it.
You shouldn’t await in _process
that will block things and be called again, this isn’t something you should do, why do you need this in this method?
I noticed that the timer await being used in the _process
function is what’s blocking it. Just thought there would be a way around it.
Can i create a function outside of the process and then pass n
as a parameter and then await the n
in that function before printing it?
If you await in _process
it will just be called again next frame while waiting, please explain why you need to await in this function, there’s really no reason I can imagine to do so, so describing what you’re trying to do would help
You’re essentially doing “every time the second hand of the clock moves start a five minute timer and then do something when it goes off”
Oh, ok. I’m trying to make a Day/Night cycle system. I got it to work but it’s using real life time and I want to set up a time myself since using a real life time won’t let some players experience the full Day/Night cycle.
How I set up the Day/Night cycle:
I added a CanvasModulate
node to my scene and also an AnimationPlayer
node. I created an animation called DayNightSys
, set it’s maximum seconds to 24 (each seconds representing an hour).
I then keyed the CanvasModulate
node in some seconds of the animation, I then played the animation via code like so:
for n in range(0, 25):
# I want to wait here for 5 seconds before going to `n` next range.
await get_tree().create_timer(1.0).timeout
print(n)
day_night_animation.play("DayNightSys")
# Trying to seek the animation to this range to simulate the Day/Night cycle.
day_night_animation.seek(n)
You shouldn’t use these features for that, just track time by counting frames or other means, you should detect time not wait for it I’d say
Try using delta
in _process
for example
I’m open to suggestions on how to better implement this, good Sir
I will try somethings and let you know if it works…
Not a “sir” thank you
But that’s my suggestion, count up using delta
Just create a variable called something like time_elapsed
and add to it every frame in _process
like time_elapsed += delta
and then when time_elapsed
is large enough you reset it and do something
Ok, so, I did:
var time_elapsed: float
func _process(delta):
time_elapsed += delta
# Converting the time_elapsed to integer since I need whole numbers
var time_to_int = int(time_elapsed)
# To simply keep the timer going as I don't want the Day/Night counter to stop
if time_to_int == 24:
time_elapsed = 0.0
It’s working, but I still need a way to slow it down. The delta
timer is now wayyy too fast. Check the Day/Night cycle now, it’s so fast.
You can divide time_elapsed
:
var time_to_int = int(time_elapsed / 5.0)
Think of “5.0” as a duration in seconds.
I halved down the delta
speed rate by doing:
time_elapsed += delta * 0.1
And it’s now very slow as I want it. I will also try your method as well to see which one is slower. Thanks for helping me out…
That’s 1 tenth of the time not half, for half you’d need 0.5
Yeah, I just felt the half isn’t too slow for me, that’s why I went lower
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.