Pick an object at random

Get help using Construct 2

Post » Mon Jul 22, 2013 8:11 am

In short-

-I'd like to make a button that selects a random set of objects and set its position to some where else.

-the action will be used again on occasions, so an object cannot be selected twice. once it has been picked, it shouldn't be picked again.

Intended purpose-

card game

In detail-

Say that I have a bib pile of objects in-game, and I want to make a button that will pick one of those objects at random and stick it some where else.

my first attempted was-

"I'll make a global variable, and when I click a button, it will set the variable to a random whole number between x-y values. If the value equals the UID number of an object, that object will then be set to position a."


-if you have about 100 objects, it would be insanely tedious to make a statement that says, "if UID is equal to this global variable, then set position here." for each and every object.

-once an object is pick, it should not be able to be picked again, this method will not do that because it will still pick any of the same values it used during the last action

Any suggestions?
Posts: 5
Reputation: 262

Post » Tue Jul 23, 2013 2:19 pm

Try using less objects. like only one and make the back of the card frame 0 and the frame 1 for value 1 etcetera.

make the random global variable and set the animationframe of the card to the variable.

I'm not sure if this helps you, but you could just say set card to position a, set animationframe to globalvariable.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
Posts: 6,160
Reputation: 19,775

Post » Tue Jul 23, 2013 8:38 pm

Hi @ry2009,

From your description, I presume that your objects are all copies of the same thing!

In which case you could add an instance variable to your object, and set its default to 0. In the system events select the 'Pick random instance' option. This will pick a random instance of any object you specify. Then add a second condition to the 'Pick random instance' event, so it will only pick an object if its instance var is = 0. Once your object has been picked, set its instance variable to 1.

Hope that helps.
Posts: 494
Reputation: 8,252

Post » Wed Jul 24, 2013 4:37 am

Thank you for your replies and suggestions.

I was able to figure out the issue. So for future reference, based on Blacksmith's suggestion, it would have been nice but the objects I was using where not all the same. It was more of a mix, like having four blue cards, three red, and two green. So it couldn't be applied in this case, but I than read up on how to create a family. What it does is sort of create a hierarchy of objects and so if you assign the solid behavior to the family, all of the objects in that family will have that behavior. After that, I was then able to apply Blacksmith's suggestion to the family. Works like a champ!

Posts: 5
Reputation: 262

Return to How do I....?

Who is online

Users browsing this forum: RetroSpock, skrotar and 8 guests