for each + every random sec

0 favourites
  • 7 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hi there!

    I came across a problem whilst working on my rpg project and I hope you could help me to fix it.

    I got enemies which should every now and then (~2sec) spawn a firebolt.

    To shorten up my code i created an 'enemy' family.

    I thougth it would be the best way to do it like this:

    System.forEach(enemy) && System.everyRandomSeconds(X)




    But instead of spawing a firebolt every X seconds individually on every enemy, it resulted in something unintented. All enemies are now spawning firebolts at the exact same time (every X seconds), which is leaving the player no chance to survive the massive amount of firebolts.

    However I want them all to attack (=spawn firebolts) seperatly, every one of them in it's own X seconds rythm. How do i need to modify my code to do this?

    I hope it became somewhat clear what I was trying to achieve. <img src="smileys/smiley5.gif" border="0" align="middle" />


    <img src="" border="0" />

  • You could use an Instance Variable to control if an enemy can fire and then apply a System.Wait for each enemy



         enemy.ReadyToFire == true


              enemy.ReadyToFire = false -- so it doesn't just do it again next Tick

              System.Wait(random(1,3)) -- Wait random time

              System.SpawnObjectAt(enemy) -- Spawn Firebolt

              enemy.ReadyToFire = true -- Ready to fire again



  • Use the Timer behaviour. It's superior to Wait or Every X...

  • Thank you for anwsering that quick!

    I got it working now, didn't use Timer behaviour, tough. If I use the "Timer" as a family behaviour I would always want to start a new "Timer" when an enemy spawns since the one family "Timer" does only trigger for the instance which were on layout when i first started the overall family "timer"? Or am I missing the point here?

    Nevertheless I found a suitable workaround. Used 'every x seconds' methode from here:

    But again: Thank you both a lot!

  • Why give the timer behaviour to the family?

    Why not to the object itself?

  • Would that make a difference?

    I mean i would still have to start them separately or this there an option to start the timer when the object is created?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I used to start a timer for the specific uid i.e.

    -> Start Timer -> Tag "My_Timer_" & Self.UID

    -> On Timer -> "MY_Timer_" & Self.UID

    don't know if that is any good, just my 2c.

    And have the duration ... + random(...)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)