# How do I make this % chance work? [SOLVED]

• I have an event that - when the player destroys a box - compares two values to decide whether a coin or a weapon should spawn.

Easy.

On box destroyed, compare two values, right?

if value 100 < 20, spawn a weapon.

if value 100 > 20, spawn a coin.

Works perfectly! However, there are multiple weapons in the game, so I thought I should add a second 'compare values' under the first event. So,

• if value 100 < 20

- if value 5 = 1 - Spawn blue gun

- if value 5 = 2 - Spawn green gun

- if value 5 = 3 - Spawn red gun

- if value 5 = 4 - Spawn rainbow gun

- if value 5 = 5 - Spawn orange gun

I figured this would work, but every time I break the box, it spawns the final gun on the list - the orange gun. No matter what, it's always the orange gun. Does anyone know what I'm doing wrong here?

• hmm... the only thing i could think is that value 5 is somehow always set to 5? try checking to make 100% sure you're got it set to randomly choose 1, 2, 3, 4, or 5. otherwise, i dunno what the issue could be

• That's the issue yeah, I have no other events influencing these ones, and I've reproduced the issue in another project. The only explanation is that it's always set to 5 for some reason, not sure why.

These are the events I have set up:

• if value 100 < 20

--- compare two values: if 5 = 1 - Spawn blue gun

--- compare two values: if 5 = 2 - Spawn green gun

--- compare two values: if 5 = 3 - Spawn red gun

--- compare two values: if 5 = 4 - Spawn rainbow gun

--- compare two values: if 5 = 5 - Spawn orange gun

Perhaps Construct doesn't allow the 'compare two variables' to be done after another? I'm not sure, I must be doing something wrong here...

• that seems like it should work. what is it that sets the value of 5?

• You need to check the number stored in "value", as the problem may be how that is calculated. Also, are you using any method to ensure it is an integer? If the number in value is a float, the "=" may never trigger and default to something.

Also what happens if the value is between 6 and 19? What gun does it spawns?

It's really hard to figure out what is wrong without a capx.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• I think you're misunderstanding.

Remember, first it checks whether it should spawn a gun or coin.

To do this, I did:

Compare two values 100 < 20 = Spawn Gun

Compare two values 100 > 20 = Spawn Coin

Meaning the coin has a 80% chance of spawning, and the gun has a 20%.

If the value is < 20, we spawn a gun. But which gun? There are 5 guns in the game, so I figured I'd compare another value.

So, if Compare two values 100 < 20, we'll check another value to see which gun it should spawn.

In the event sheet, it'll now look like this:

• if value 100 < 20

--- compare two values: if 5 = 1 - Spawn blue gun

--- compare two values: if 5 = 2 - Spawn green gun

--- compare two values: if 5 = 3 - Spawn red gun

--- compare two values: if 5 = 4 - Spawn rainbow gun

--- compare two values: if 5 = 5 - Spawn orange gun

• if value 100 > 20 - Spawn coin.

Oh, I'm talking about the compare two values system expression.

• Found the issue, completely slipped my mind.

I needed to add 'random' in front of the first value, it was always going to 100 because it was set to simply '100', instead of 'random(100)'.

• if value random(100) < 20

--- compare two values: if random(5) = 1 - Spawn blue gun

--- compare two values: if random(5) = 2 - Spawn green gun

--- compare two values: if random(5) = 3 - Spawn red gun

--- compare two values: if random(5) = 4 - Spawn rainbow gun

--- compare two values: if random(5) = 5 - Spawn orange gun

• if value random(100) > 20 - Spawn coin.
• You need to assign the random value to a variable, so it is only generated once. Then you compare the one random value to decode which range it falls into. You should really use Else as well, so the compares stop when the correct value is found.

You need int(random(5))+1 to get a range of 1 to 5. Or, int(random(1,6)).

• ceil(random(0,6)) i suggest.. this round up the number

• I've solved the problem already but thanks.