Newbie here, question with arrays and list of objects

Hello!
I am developing a bit of a puzzler game, where I set the items in a grid.
The object is placed correctly, my grid is 8x8, all done using a sample I found on the forum.
But the thing is that I have 8 objects to create, randomly.

So, how can I setup a list of the objects I want to create and shuffle it so I can then traverse it in the repeat event?

thank you.

In case you mean you have 8 different objects and you want to create a random object each time, you have 2 options.

  1. Add each object to a group for example “zombies” and name each object the same with an unique number at the end

group ZOMBIES -zombie0 -zombie1 -zombie2

And so on.
When you want to create a random object, first, generate a random number using the Random() expression and store the number in a variable

Do =Random(7) to variable zombie_id

Then, create the object “from it name” and use the variable to pick a random object from the group.

Create object from it name "zombie" + ToString(Variable(zombie_id)) in group "ZOMBIES"
  1. The other, easy option if you just want it to look different and the object got no animations, you can add each image as animation of the same object and simply pick a random animation when you create the object.

Create object "zombie" Do =Random(7) to animation of zombie

That’s it. Note that, I use Random(7) above, because 0 also included, this way you get a random number from 0 to 7… and use “zombie” as example because why not…

Hope it helps.

Of course I left something unexplained…
My game is a matching game, where you just match a couple buttons and then they disappear.
8 buttons on a 8x8 grid.

I made a huge list of 64 values, and then proceeded to make it random. But I believe I’ve failed in doing so. I used the card shuffling example in the wiki, but I have failed.

Please note, I can make a grid of the same item, I just need to make it a random list of objects.

I think I can create a list of children, and find them by index, creating it from the child value that would be it’s name?

Something like:
Buttons
button0 “button0”
button1 “button1”
button2 “button2”
button3 “button3”
button4 “button4”
button5 “button5”
button6 “button6”
button7 “button7”
button8 “button0”
.
.
.
button63 “button7”
(It isn’t that bad, I’ll just edit the scene file XML and do a bit of copy-paste)

And then shuffle this somehow, and then traverse this list creating an object from the value’s name, using a counter…
Is this possible/feasible?

Maybe I misunderstand it again but in case you want to create a matching game, like find 2 identical buttons or something, instead of creating the whole grid of objects on the fly from different objects, I would just simply create a grid of objects in the scene editor using instances of the same object and simply change their animations.
At the beginning of the scene you could go through each of them, set their type using object variable with a random value and use a scene variable to control maximum how many times a certain value can be used as type. In case you reach the limit, the value can not be used any more and need to find an other random value again while you find one that is free to use.
Finally, when you click a button, just set it animation using the value of the type variable.
Regardless what each “type” looks like visually, internally just represent each type as a number which is corresponding with the animation numbers. So when you click any button, to display the correct animation you can just do

If button is clicked : Do = button.Variable(type) to the animation of button

And that’s it, it should work on every single button. Just get the type of the first button clicked and the type of the second button clicked, compare, if the value match just keep them as is and don’t allow them to be clicked again, if it different then change their animation back to default and can be clicked again to find the matching pair.

How should I write the line “Create object from…”. I can’t seems to create this line.

I don’t know where should i place ToString at.

Please stop necroposting. Create your own posts. And again, 3 years old solutions were using different technologies and should not be claimed functional on newer versions. Here, it actually works, but you didn’t understood it apparently. What you need to do is to add the ToString expression after the name of the object to create.

1 Like