[Solved] Pick a random instance other than the one being interacted with

UPDATE: [Solved] See the solution here: [Solved] Pick a random instance other than the one being interacted with - #18 by BWPanda

I have the following setup:

I want to make it so that when you click on a box, a different box (chosen at random) turns green.

I’ve tried various ways of doing this. This is what I have currently (not working):

Any ideas what I’m doing wrong and how best to achieve this?

EDIT: To clarify, the box you click on should never be the one that turns green.

I think the problem is that the first event filters the list of box objects that GDevelop works with down to just that one box.

To counter this, to the first event add the action Pick all instances of box. I think that should solve the issue.

I think this is one of the things I tried originally, and just did so again, but unfortunately still no luck…

I talk this with MrMen before, just a little question, in first event, when the cursor touch a box, need to “see” the next event (That because events distribution).
You have some problem to change “Left mouse button was released” to “The Cursor/touch is on box”. That makes, every time the cursor is in one box, don’t activate any event (Because you don’t touch the left mouse)

And what that important in this post? (I don’t know why didn’t work) but why you need pick all box??

No, I wrote :

And if the action doesn’t work, try is as the first subevent, and make the while event a subevent of that.

@MrMen Unfortunately still not working (tried both your suggestions from the last comment)… I have tried to simplify it a bit (removing the ‘OR’ condition). Here’s what I have currently:

Since I’m no longer checking if the randomly-chosen box has already turned green, I’m now also trying to rotate the selected box to better see if the same box is selected multiple times.

But it’s not working at all (nothing’s turning green or rotating). I’ve tried various other events/conditions/actions, but nothing so far selects a random box from those where the mouse isn’t.

So this one seems to be working:

However it only works sporadically (sometimes for a few clicks, sometimes not at all). Not sure what’s going on, but I don’t think I want to use this in my actual game until it works reliably…

EDIT: Here’s a screencast of the current issue:

Peek 2022-08-11 14-32

Hey there! So first off, i would get rid of the while loop. In this case its not necessary and may he causing some issues for you.

So, your first event should check if the mouse is hovering over one of the boxes. Have an instance boolean that is set true when the mouse is hovering over it. And set to false when the mouse is not on it.

From there you can have it check what the animation currently is as a sub-event and see if the box has already been turned green. As well, have another sub event under the mouse click that checks the animation as a red box. That will have its own sub event that will pick a random box and, as long as that instance boolean is false, set it to the next animation and rotate it.

This will make sure it will never pick the box you are currently selected to rotate and change animation.

Since this will happen every time you click, there is no need for the while loop. Let me know if you need as visual aid and ill put something together tomorrow. Good luck!

Thanks @CTXB. Based on your description, I came up with this:

And while it does work in terms of not allowing the clicked box to turn green, it also means that sometimes when you click on a box, nothing happens. This is because the clicked box was randomly selected, but the conditions prevented it from turning green.

However I need it so that clicking on a box will guarantee that a different box turns green. I can’t have it where clicking on a box does nothing… This is the purpose of the while loop above - to randomly pick a box, and if that box has the mouse over it, randomly pick another box. Repeat as necessary.

Hope that makes sense.

I gotcha. Just move the “left click check” out from the “set active” action. That part shouldnt be a sub event. And move the “pick a random” down to the event that checks if the box is active. Since the “pick random” happens before the “check if active”, then it will sometimes pick the active box and then not change it.

Like this?

In this case, it now sometimes turns green the box you’re clicking on… So still not working.

No, it’s looping while the picked box is the same as the selected box. Which is a good way of approaching this.


And this is what I’d do too. The reason it sometimes appears not to work is because you’re selecting a green box. So add another condition to the while loop (OR it with the cursor is on box) that also checks if the box animation is ‘green’.

Since there is only one thing occuring at the time of the click, there is no need for a while loop. Their checks are not in the right order. Ill post an example later on once my computer is set up. All they would need to do is check the active state of the boolean and then check the animation in the same check. That would pick a random in that range. There is no loop since nothing is continually happening. If the checks were right, it would never pick the actibe block.

The loop is continually picking a random box until it no longer satisfies the conditions.

So he wants it to keep changing the boxes, even if he just clicks once? I guess i would need to know the implementation to further help. If he wants it to continually change the boxes even after one click he still needs to make it check the current animation status so it doesnt pick an already green one.

I’m not sure that’s the case… Since if I click enough times, it’d eventually pick a red box and I’d see it change. But once it stops working, it stops working, and no amount of clicks change anything from that point on. Additionally, if I leave the preview window open for a while after it stops working, I hear my computer fan get loud, which makes me think it’s stuck processing some kind of infinite loop…

In any case, here’re the current events:

This works :

2 Likes

Thanks for that @MrMen. I can confirm it works, however I’m a bit confused by the logic… It seems that in the second event (first sub-event) it picks all boxes, narrows it down to the red ones, then narrows it down to the ones without the mouse cursor over them. It then pick a random box from that selection. Great! But then it goes into the while loop to see if the cursor is over the box. As far as I can tell, that’ll never be the case…

So I played around with this myself and simplified it down to this:

Surprisingly this works, and there’s no loop! I didn’t need to worry about whether a red or green box is clicked on, so I removed those conditions.

Thank you very much @MrMen for your assistance and patience, and thank you also to @CTXB for proposing the idea of being able to do this without a loop (sorry we didn’t believe you :smile: ).

Lmao you’re fine! I would have made an example on my computer but it wont be hooked up for a day or two. Im glad you got it to work though! I hope i was of some help!

1 Like

Yeah, sorry. It was written at 2 in the morning… :expressionless:


That does simplify it, and it’s good to see you worked out and have shared an efficient solution. It won’t be the only time someone will want to choose an object instance bar a specific one.


Your suggestion made me look for a way of doing it without the use of an instance variable. Definitely a push in the right direction, and @BWPanda finished it off.