Use a scene timer in a while loop

How do I…

I’m trying to create objects with a delay of a second. I utilized a scene timer, but I’m obviously missing something.

What is the expected result

The action is repeated x times with a delay of 1 second.

What is the actual result

Gdevelop gets stuck and does create a single instance.

Related screenshots

You don’t need that “Trigger once” condition. Once the timer is over 1 second it gets reset. That may be causing an issue…

Thanks, I removed the condition, but the whole “while” block does not get triggered since the log message “creation of instance started” does not even appear within the logs. I used timers before, I’m really puzzled.

What does debugging the value of the parameter toCreate produce?

ToCreate = 3

For the time being, it’s hardcoded. If I remove the timers and related conditions, the code creates three instances. But all at the same time, I want that to happen with a delay.

I’ve never had much luck with timers using while loops, an alternative is using the repeat every X seconds X times event of the RepeatEveryXSeconds extension. In practice you can achieve the same effect that you’re trying to do with a while loop and scene timer.

Thanks for the hint on this extension. I installed it, but I still can’t get it to work. Here’s what I’ve done:

Again, the whole block that creates the instances is not being triggerered. I created the timer “spawn” as a scene timer further up.

You can use the repeat every x seconds event without making a timer, in fact that could be the reason it’s not working. Try getting rid of the event where you create the scene timer and see if that helps. If it doesn’t work, are you certain there’s not a problem with the structure variables?

@weadsy1, I’m not sure if I understand you correctly. The extension does not contain any actions, only two conditions (one is the one you are referring to, “Triggers every X seconds X amounts of time”).
If I use this one, it does create a timer (which is running, I can see this in the debugger).

However, nothing is being spawned. Here’s the current implementation (without creating the timer upfront):

Removing the second condition doesn’t make a difference.

It contains 2 condition functions that also run events, which start and repeat the timers. Thus creating events outside the extension (in your event’s sheet) that control the same timer could possibly interfere with the extension’s events.

If it’s still not working, then are you sure the condition is evaluating to true? If it is then are you sure the formula that returns the X coordinate is working properly?

Additionally, is this extension being ran every frame?

Regarding the first condition: not sure, tbh. I’m not sure how this is supposed to be used.

The second condition is definitely true.

The x value is getting calculated correctly and the code works as expected when I remove everything related to timing - three instances are being created in the right position.

I just can’t figure out a way to delay the creation of the latter for instances.

Is the extension being ran every frame then?

Sorry I missed that point.

The function is being triggered by the value of a variable having a certain value.
=0 → prepare the stage
=1 → create the instances (the step I’m at right now)

and so on.

Since status 2 is not implemented yet, it remains on 1. Does that suffice to run every frame?

Is this a lifecycle function? If it is do you mean that is what is setting the variable, or is the extension itself ran when a variable in the events sheet is a certain value?

Not sure what you mean by lifecycle function.

A button calls the function “gather” (in my extension).

This function sets the status to 0.

There’s a condition for status=0 which calls the prepare stage function. The end of this function sets the status to 1.

Then, there’s a second condition for status=1 which calls the create instance function. It looks like this:

Could you put a wait in the actions instead, and use a repeat event?

I solved it by moving it to the behavior as well. I use wait and set the time with the counter (-> 0 = don‘t wait, 1 = wait 1 second and so on).

But I still don’t understand the problem.

I don’t either. Your first post looked like the logic is correct. Unless I’m missing something, it may be that GDevelop has a bug.

1 Like

My 2 cents. I don’t believe time passes within loops or even the frame that the code is on. I believe some external values like keyboard, mouse and time are only updated between your events. They’re the same value at the beginning of the frame that your code executes and at the end.

While loop events explicitly stop any other events from proceeding until the loop conditions are false, and never process past the current frame until they are done. I don’t believe timers or waits will ever work in while loops due to this.