How to wait a few seconds in for loop

Godot Version



I have a very noobie question. I have this simple for loop:

	for n in range(0, 25):

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

1 Like

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?

1 Like

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”

1 Like

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
        # Trying to seek the animation to this range to simulate the Day/Night cycle.

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

1 Like

Try using delta in _process for example

1 Like

I’m open to suggestions on how to better implement this, good Sir :bowing_woman:

I will try somethings and let you know if it works…

Not a “sir” thank you :slight_smile:

But that’s my suggestion, count up using delta

1 Like

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

1 Like

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.

1 Like

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…

1 Like

That’s 1 tenth of the time not half, for half you’d need 0.5

1 Like

Yeah, I just felt the half isn’t too slow for me, that’s why I went lower