How Do I Check If Variable Holds a Certain Number

Get help using Construct 2

Post » Sat Apr 30, 2016 5:47 pm

I have scoured the forums, but couldn't find an answer. Here is the problem: I have 10 different sprites that are used for push buttons. I need each sprite to have a random number (1-10) assigned to each sprite variable. The problem I am having is that I don't want there to be duplicate numbers spanning the 10 sprites. Is there a way to check each sprite variable and if there is a number that is in another sprite variable it randomizes again until the sprite has a number not being used. The first sprite variable being assigned a random number is not a problem because that is always the starting sprite variable that does not need to be checked, all the following sprite variables need to be checked. This happens until each sprite variable has a number 1-10 without duplicates. I am sure there is a easy answer to this that I am missing. I appreciate any help.

Thank You In Advance
InsertCoin25
B
4
G
1
Posts: 17
Reputation: 530

Post » Sat Apr 30, 2016 5:55 pm

ImageImageImage
B
71
S
21
G
229
Posts: 3,686
Reputation: 128,336

Post » Sat Apr 30, 2016 6:00 pm

There are different solutions (like helper objects that disappear, instance variables or loops that check if already used)

But the best one would be an Array. Here you randomize your Array and pull a number from it (see tutorials for Arrays)

  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Sat Apr 30, 2016 11:35 pm


  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Sat Apr 30, 2016 11:55 pm

Thank you blackhornet and fm4fanAT. I really appreciate the help. I have been trying blackhornet's plugin, but can't figure out how to set the random numbers to the variables.
I am going to give the array tutorial a try as well. Thank you both for the help.

InsertCoin25
B
4
G
1
Posts: 17
Reputation: 530

Post » Sat Apr 30, 2016 11:57 pm

The Next expression will pull out the next random number.
ImageImageImage
B
71
S
21
G
229
Posts: 3,686
Reputation: 128,336

Post » Sun May 01, 2016 3:55 pm

OK I am still having a problem understanding this. Say I have variable $PushButton1, $PushButton2, $PushButton3, etc. all the way to $PushButton10. Will the Next Expression fill these variables with the random numbers generated without duplicates in order? Just trying to wrap my head around how the expressions and code work in the plugin. Thank you for the info in advance.

InsertCoin25
B
4
G
1
Posts: 17
Reputation: 530

Post » Sun May 01, 2016 5:10 pm

Yes. You'd be better off using an array, but this will work:
PushButton1 = BHTSmartRandom.Next
PushButton2 = BHTSmartRandom.Next
PushButton3 = BHTSmartRandom.Next
...
ImageImageImage
B
71
S
21
G
229
Posts: 3,686
Reputation: 128,336


Return to How do I....?

Who is online

Users browsing this forum: Unconnected, Yahoo [Bot] and 23 guests