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

Get help using Construct 2

Post » Mon Oct 10, 2016 2:28 am

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?
Last edited by ChaoticShadow24 on Mon Oct 10, 2016 4:12 am, edited 1 time in total.
B
25
S
10
G
1
Posts: 154
Reputation: 2,687

Post » Mon Oct 10, 2016 2:47 am

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
B
15
S
8
G
2
Posts: 10
Reputation: 2,434

Post » Mon Oct 10, 2016 2:55 am

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...
B
25
S
10
G
1
Posts: 154
Reputation: 2,687

Post » Mon Oct 10, 2016 3:02 am

that seems like it should work. what is it that sets the value of 5?
B
15
S
8
G
2
Posts: 10
Reputation: 2,434

Post » Mon Oct 10, 2016 3:07 am

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.
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Mon Oct 10, 2016 3:11 am

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.

Hopefully I made sense.

Oh, I'm talking about the compare two values system expression.
B
25
S
10
G
1
Posts: 154
Reputation: 2,687

Post » Mon Oct 10, 2016 4:12 am

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.
B
25
S
10
G
1
Posts: 154
Reputation: 2,687

Post » Mon Oct 10, 2016 5:29 am

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)).
ImageImageImage
B
71
S
22
G
249
Posts: 3,763
Reputation: 138,577

Post » Mon Oct 10, 2016 9:59 am

ceil(random(0,6)) i suggest.. this round up the number
B
79
S
31
G
7
Posts: 148
Reputation: 9,179

Post » Thu Oct 13, 2016 1:29 am

I've solved the problem already but thanks.
B
25
S
10
G
1
Posts: 154
Reputation: 2,687


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests