How do I - generate random number of enemies

Get help using Construct 2

Post » Mon Dec 26, 2016 3:41 pm

Message: falzona can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
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.
https://gyazo.com/51e09a56b90e1fb59ba43d374e978c58
B
2
Posts: 1
Reputation: 152

Post » Mon Dec 26, 2016 6:24 pm

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]
The LEVEL GENERATOR TOOLKIT



24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 242
Reputation: 2,306

Post » Mon Dec 26, 2016 6:34 pm

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Dec 26, 2016 11:34 pm

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.
B
42
S
14
G
3
Posts: 165
Reputation: 5,115


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, jorgmaquoi, kiki4construct and 33 guests