How do I Create a Delay In a Loop

Get help using Construct 2

Post » Sun Aug 14, 2016 12:56 am

Hello,

I have an array:

For "x" from 0 to 100
For "y" from 0 to 100

Condition: Is overlapping "object"

Action: Create object at loopindex("x"), loopindex("y")


In a nutshell, I have a randomly generated shape that I spawn at a location. I find out the boundaries of this shape and then run an arrray at the same coordinates of that shape. It basically spawns a sprite on a 32 grid snap wherever the object is overlapping with the shape within that boundary.

My problem is sometimes these shapes can be as may as 600 sprites. When the loop runs, spawning all 600 in one tick freezes the screen until it is done. Currently I have this function running during the game. I want to eliminate the freezing whenever these groups of sprites get generated. I tried put in a "wait", but it doesn't same to wait in between creating each sprite. Maybe someone else has a better idea how to spawn these sprites so that the screen doesn't freeze.

Thanks!
B
30
S
14
G
3
Posts: 16
Reputation: 3,734

Post » Sun Aug 14, 2016 1:52 am

'wait loopindex * 0.01sec' will introduce a 0.01 second lag between each repetition of the loop.

Make sure you put the wait function in front of the other actions in the loop.
Mega Game Studios is a tiny independent game studio run from our apartment in Melbourne, Australia. We're not quite sure what we do yet but we think it has something to do with apps, board games and plush toys.

www.megagamestudios.com
B
18
S
7
G
3
Posts: 85
Reputation: 2,920

Post » Sun Aug 14, 2016 10:36 pm

Hello,

So I tired the delay in the loop above all actions, did not stop the screen freeze at all.

Any other suggestion on how I can spawn so many sprites without overloading the CPU?

Would appreciate any help. If I can get this accomplished it is a pretty major milestone in getting to the next phase of my game done.

*Update*

Actually I lied. I have managed to get it to work. I did it differently though. I put a 0.01 sec delay after checking for collision over top of the shape and "destroy" if not part of the loop. Then put another one after disabling collisions on the node.
B
30
S
14
G
3
Posts: 16
Reputation: 3,734

Post » Mon Aug 15, 2016 9:57 am

Hi tried something similar, but I have a funny effect...

This is the "wrong" situation, that is without the wait:

Image

It does spawn in the right direction, but in the wrong number:

Image

If I put the wait as first action (just don't look at the txtdebug, it acts the same way without), like this:

Image

It spawns the correct number of sprites, but in the totally wrong direction!

Image

I know that I am missing something very obvious, but I don't get what...
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
30
S
13
G
4
Posts: 141
Reputation: 4,832

Post » Tue Aug 16, 2016 7:30 am

Any idea somebody?
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
30
S
13
G
4
Posts: 141
Reputation: 4,832

Post » Tue Aug 16, 2016 9:13 am

@Jeff Skyrunner: You should post your capx in a topic of your own.
The actual informations you are giving are not enough at all to help explaining what is going on.

You mention a wrong number of instances being spawned, that apparently has to do with the value of iNumLaser, since it is used as boundary in your loop, but we don't know where it is coming from.

And when you delay, you are actually using the wrong "iTargetX" value, since you delayed the command, but the actual ShipSelected coordinates used is not delayed.
Either save the actual coordinates to use with the same time offset (like in an array in wich you would push coordinates, and read them when the wait command has finished kicking in.)

@Maydie: Unfortunately, I'm afraid you can't really prevent the freeze caused by numerous instances being created/destroyed in a single tick.
A way to cheat, is to have the spawn occur over several ticks instead of a single one, but I'm very unclear on what you are exactly attempting to do in the first place;, so I'm not sure this would fit.
Nevertheless, it is a bad design in the first place to destroy/Create a lot of instances in a single tick, and you should modify this idea first in order to be able to implement something more appropriate.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
289
S
112
G
94
Posts: 7,333
Reputation: 69,287

Post » Tue Aug 16, 2016 1:43 pm

@Kyatric

Sorry, I thought that being the same problem it would be better to use the same thread: my bad.

Btw, thank you very much: you point me in the right direction! The error in the delay was that I was using local variable (iTargetX) instead of global ones (the target is always the same, so no need for an array).

iNumLaser is correct: the "error" was just that being spawn all together, the sprites were all overlapping.

Many thanks and sorry again for the error of using the same thread.
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
30
S
13
G
4
Posts: 141
Reputation: 4,832

Post » Tue Aug 16, 2016 1:56 pm

@maydie can you have the sprites invisible and turn them visible when needed? Or have them off screen and set their location? I don't think spawning 600 sprites at once is a good idea
ImageImageImage
B
39
S
22
G
11
Posts: 492
Reputation: 9,925

Post » Wed Aug 17, 2016 12:59 pm

@Jeff Skyrunner: It is not exactly the same problem.
It has to do with loop and using the "wait" command. But the OPs issue is about spawning a massive lot of instances that freezes the execution when done in a single tick.
In that, you issue was pretty different.

Nevertheless, nice to know you manage in fixing your issue thank to this topic in the end.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
289
S
112
G
94
Posts: 7,333
Reputation: 69,287


Return to How do I....?

Who is online

Users browsing this forum: plinkie, Yahoo [Bot] and 18 guests