How do I - generate random number of enemies

0 favourites
  • 4 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hi, i am working on a project, in this project i would like to have it scale in difficulty as he passes through levels. to do this one of the things i want to do is generate a random number in the beginning of the layout and then spawn an enemy every second until he reaches that number. so far i have tried multiple ways to do this but they have either resulted in no spawns or infinite loops.

    gyazo.com/51e09a56b90e1fb59ba43d374e978c58

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • A few ways come to mind. The simplest one to me seems to be:

    On layout start

    For "enemies" from 0 to EnemyCount

    Wait loopindex("enemies") seconds

    Create object [Enemy]

  • On 'Start of layout' happens 1 time and in the time of 1 tick.

    A 'for' loop happens in the time of 1 tick.

    So, the system will once and only once visit that "every second" condition.

    To make that 'every second' work, the system needs every tick access to it, the system will look if its time to fire its events, if not it moves on.

    You need two global variables.'lvl2Rand' (=zero) and 'Counter' (=zero)

    In that On Start of layout event, you can set lvl2Random as you did.

    Outside that event, and as a top event :

    On every (1) second.

    _____sub event : System compare 2 values ... 'Counter' ... is < ... 'lvl2Rand'.

    _____Action .. spawn 1 object

    _____action .. add 1 to 'Counter'

    Now the systems eye will look into that condtion 'every second' each tick. When its time (1 second) if will fire the sub events. When it is not time yet, it will skip that event. That is how it does that Every x seconds.

  • On start of layout

    Set enemyMaxCount = int(random(x, y)) //sets random round number between x and y

    Set enemyCount = 0 //this will increment

    Every 1 seconds AND if enemyCount < enemyMaxCount

    spawn enemy

    At the start of the layout, it sets a maximum number of enemies to spawn. Then it increments another number starting at 0 until it reaches the max, but only once every 1 second. This should work.

    Edit:

    mrtumbles has the right idea though. If you're not worried about conditions (the wait function cannot be canceled, so once you call something which is delayed, it will happen whether you like it or not, but it's simple) then that's the best way I think.

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