How do I choose a number from 1-5, but not the last chosen number?

0 favourites
• 11 posts
From the Asset Store
Show your master of martial arts by killing all flies empty handedly!!!
• Each time an enemy is generated, its position is chosen from 1 of 5 positions using an instance variable:

Set enemy_position to floor(random(1,5))

Ideally a new enemy wouldn't be positioned in the same place as the enemy before it (although it would be fine for the enemy after this one to be placed in that original position so it doesn't need to fill each position 1,2,3,4,5 before repeating, just not in the same place twice, one after the other).

I set enemy_position to a global variable 'lastgenerated' so am really looking for a command along the lines of

Set enemy_position to floor((random(1,5) excluding lastgenerated)) -- which, of course, doesn't work - but that's the kind of logic I'm hoping exists without having to create an array, or AdvancedRandom etc.

Is this possible?

Many thanks :)

• The most straight forward way would be to

Set enemy_position to floor(random(1,5))

If enemy_position == lastposition, do nothing/do again

Else, do things

It's not particularly computationally expensive since you only care about the last picked number, and statistically the same number won't happen many times in a row.

• Thanks oosyrag for your reply and suggestion!

I've tried several ways of doing what you suggest, but aren't getting it (unusual results abound).

Here's the enemy generating event - where would I put the if/else etc?

Probably easiest if I just post the c3p - might make it simpler to see what I'm trying to accomplish :)

Thanks again!

dropbox.com/s/003ws4j0fa7wz9s/Helmet.c3p

• I would use a while loop

while enemyPosition = lastEnemyPosition

set enemy position to random(min,max)

• Advanced random plugin has a permutation table function. It does exactly what you want

• Thanks to all RadioMars, fedca, oosyrag for your suggestions.

Having spent several hours trying to adapt demos and follow tutorials to make this fit my use case, none are working unfortunately. I guess my brain just doesn't logic this way, but I thank you for your efforts to help.

• There are certainly more elegant ways to do this (mentioned by other users above), but this is one way to solve it. It introduces a new variable called "next position" for easier comparison.

• Try Construct 3

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

Construct 3 users don't see these ads
• Oh wow - brushfe you've nailed it! I doubt I ever would have got there without your example (the subevent / order I'd have struggled to get my head around) - much appreciated, thanks so much!

oosyrag your example is also excellent - thanks! I think I got something close to this but in my use case, the enemy generation didn't work like your example does so might have been in the way I was ordering it, or something was cancelling it out.

In any case, thanks to everyone who helped out with this - great community - and I can save AdvancedRandom for another day!

• Good to hear you've got it sorted! I think oosyrag is right about AdvancedRandom; it took me a while to get into it (the tutorials on this site are great), but it's amazing for generating interesting gameplay situations in an elegant way. Hopefully you can dive into it some time!

• Permutation tables are not as scary as they sound. Here is a demo:

By the way, if you need a number from 1 to 5, this is the correct expression:

floor(random(1,6))

• 11 posts