... make a random object out of a group appear?

Hello people,
I’m trying to make the computer choose one of the buttons randomly. I have created a group with nine buttons. The idea is that if the user is too overwhelmed to look at and choose one of the buttons, the programme will do it for them. (It’s a mental health game). So they can click ‘please choose for me’ without looking at the buttons in the group and then only one of the buttons from the group should appear that they can then click or go back. I tried the ‘Pick a random’ function and included the Group, but it is not working. After researching in the forum I also tried the create object function, but that didn’t work, either. Any tips?
Thank you!!

Put all the button names into an array. Then pick one of the array entries by usnig a randomly generated index (with Random(VariableChildCount(button_name_array) - 1))

Hi Mr. Men,
thank you so much for your comment. I’ve created an array and added the button names as string variables to the array as children. But where can I create the randomly generated index? The only thing I’ve found is ‘add existing variable’ under ‘arrays and structures’, but I can’t find a ‘random’ option or anywhere I can add the code you sent.
Any more tips?
Thank you!
Lou

You have an object group that has all the buttons (say named Button_group), an array with all the button names (say named button_name_array and with children that match the button object names) and you have a random index (as per my previous post).

You then use the “Create an object from its name”:

Hi,
thank you so much for your reply. I’m afraid I don’t understand how to create a random index. I don’t know where to enter the function (is it a function?) you wrote in your previous post.
Thank you!
Lou

To generate a random number, you can use the random() function. It takes a min and a max. In your case, it would be random(0, VariableChildCount(Array) - 1). Replace array with your array. Variable child count gets the length of a struct or array.

Random(number) takes one number. It generates a number between 0 and a number.

RandomInRange(number, number) takes 2 numbers, a lower and an upper. It generates a number from the one number to the other.

You can create an object using a random number to choose from an array with the list of object names.

Another option is to name your objects with the same name plus a sequential number like turtle1, turtle2, turtle3 and put them into a group. You could then use:
Create an object by name “Turtle” + ToString(RandomInRange(1, 3)) from the group

https://wiki.gdevelop.io/gdevelop5/all-features/expressions-reference/#mathematical-tools

The random index is just a randomly picked value that is stored in a number variable.

So, using my earlier response, it’s:

image

Does it work though?

The Random(Number) function? Yes, it returns a value between 0 and the number passed. It’s the same as RandomInRange(0, Number).

I was asking OP if our solution worked because they asked if the code was correct but never told us if they got it working

Haha, that’s what happens when there’s no context to the post. Sorry for the misunderstanding :smiley: