Is this a wrong code?

Discussion and feedback on Construct 2

Post » Mon Jun 01, 2015 2:14 am

Hi everyone
I'm using a global variable to create a random objects, But sometimes this event won't trigger, i don't know what's wrong with it:

Global variable = NumbersVar

On start of layout ------ System set NumbersVar to floor(random(1,52))

NumbersVar = 1
Trigger once -------- create object A

NumbersVar = 2
Trigger once -------- create object B

Etc...

Objects family on destroyed ----- System set NumbersVar to floor(random(1,52))

------------------------
Thanks for any help guys
B
60
S
10
G
10
Posts: 1,013
Reputation: 9,155

Post » Mon Jun 01, 2015 2:30 am

It wouldn't trigger if the same random number is used twice in a row. A possible solution that would require minimal modification to your events would be to set the number to 0 and the set it to a random number from there.

Code: Select all
Global variable = NumbersVar

On start of layout ------ System set NumbersVar to 0

NumbersVar = 0
Trigger once -------- System set NumbersVar to floor(random(1,52))

NumbersVar = 1
Trigger once -------- create object A

NumbersVar = 2
Trigger once -------- create object B

Etc...

Objects family on destroyed ----- System set NumbersVar to 0
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Mon Jun 01, 2015 2:38 am

I would expect on the start of your layout if Random() doesn't produce a 1 or a 2 then those triggers would not happen.

Are Family Objects being destroyed a lot? i.e. is that OnDestroyed event being triggered a lot?
B
97
S
32
G
16
Posts: 1,199
Reputation: 16,680

Post » Tue Jun 02, 2015 12:10 am

@R0J0hound thanks for the answer, now it's working fine.

@jobel yes, the event will trigger very lot, it's an endless numbers game.
B
60
S
10
G
10
Posts: 1,013
Reputation: 9,155


Return to Construct 2 General

Who is online

Users browsing this forum: BackendFreak, rayolaser and 22 guests