Create two random objects from a group without being duplicated

hi, I have a group of objects called power ups and at the end of each level I would like to create 2 objects of my choice taken from the group without being repeated/duplicated.
how can I do?

Put the powerups in an array and create two variables, with randominrange() assign to the variables with a while loop compare the two variables if there are equals run the randominrage again in one of the two variables. After that you create an object by name with the variables.

1 Like

forgive me, I’m not very familiar with arrays… am I doing it right?

You have to create the array with the names of the powerups

1 Like

A simpler method:

1 Like

what does random() return?

Could be an out of bound error

hi mr men, you are my savior every time!
in the meantime I was trying to learn the basics of array variables from the video linked by suh.
at the moment I practically copied and pasted the code you shared… now how do I create two objects taken from that array?

The array should have each powerup object name in it. So just use the Create object from its name action, using the PowerUp_Text variables and the power-up object group.

Random(n) returns a random value between 0 and n.

But is it inclusive if so could potentially get an out of bound error, so Random(VariableChildCount(array)-1) should be use

I’ve just checked it is inclusive so use Random(VariableChildCount(array)-1)

1 Like

My bad, thanks for picking up on that one.

sorry for my ignorance and the delay in responding… where am I going wrong?
arraygroup

Powerup_array should be a variable array, not an object group. You’ll have to create it as a global or scene variable, and manually add each object’s name. Unfortunately, there isn’t a function/action available that would return a list of each of the object group’s members.

is this what you mean?


It seems to go this way!
thank you so much guys

1 Like

forgive me if I go back to this topic… I needed to create a random ammo drop and I was using the same method.
the problem is that in this case I should change the output text when the chest is opened and not an object.
obviously I’m not succeeding hahaha
this is where I arrived, can you give me a tip


What part is not working?

when the text is created, 0 appears while I would like the name of the variable found to appear

It doesn’t work even once?

If you use that method when you remove all the children of the array at the end you’ll have nothing, if it doesn’t work even once I don’t really know I can’t see any typo or anything like that, the only thing is when you are using VariableString() in my test it’s colored the same way Random() would be and in you image it’s not, some maybe something there is wrong.

I can’t tell if the left margin is cropped but are the 1st two events sub events of something because it looks like they would run on every frame and empty the array in 2 frames.

Once the array was empty, it would generate a random number of zero and I’m guessing that since that element doesn’t exist it’s getting created at the default value of zero.