For Each applied to selection buttons

I’m sure the answer to this is very simple, but for some reason, I’m just not getting the “Repeat for each … object”. Sure I could do this with 4 actions, but I’m trying to learn the nuances of GDevelop.

Scenario: 4 radio buttons on screen. When user clicks on one, its animation is changed to name “select” and all other radio button animations are set to name “unselect”.

Set up:
There is one sprite named challangeSelectDot with 4 instances. Each instance has a variable (ID) set to the number of the button (so I know which one the user clicked on).

I created an extension (PARAM1 named dotClicked is passed the challangeSelectDot the mouse is on when clicked):
Events:
Variable ID of dotClicked = 1 Set animation of dotClicked = “select”
Variable ID of dotClicked = 2 Set animation of dotClicked = “select”
Variable ID of dotClicked = 3 Set animation of dotClicked = “select”
Variable ID of dotClicked = 4 Set animation of dotClicked = “select”

In the main Events window:
Touch or left mouse is down
cursor /touch is on challangeSelectDot
Repeat for each challangeSelectDot object Set animation of challangeSelectDot to “unselect”
(no condition) Call extension using challangeSelectDot

Results:

  • the animation change to “select” works
  • the animation change (using For Each) does not work, so on the second click, the first button clicked still shows selected.

Can someone tell me why this is misbehaving?

Thanks
Art.

1 Like

hi, maybe you have to reset the state (animation) of the selectDot sprites before the for each

Well, after a lot of frustrating experimentation, I have come up with a working solution (link to project attached).

Google Drive - radio button menu.zip

(hope this works - never tried using Google Drive before)

Lessons Learned:
When using instances of an object with Cursor/Touch, you can only reference the one instance clicked on (yeah, in hind-sight, this makes sense but sometimes I need to bash my head to understand)

Summary of code

  • test for mouse click on radioButton
    • if clicked, then
      • challangeSelected = radioButton.Variable(id)
      • set radioButtonClicked = true
    • end if
  • if radioButtonClicked = true then
    • For Each instance of radioButton Loop
      • set animation name of radioButton = “unselect”
      • if radioButton.Variable(id) = Variable(challangeSelected) /* see above */
        • set animation of radioButton = “select”
      • end if
  • end if

I would still appreciate it if an experienced GDevelop user can look at the attached mini-project and let me know if there was an easier way to accomplish my goal.

Thanks,
Art.

PS - if the powers that be want to add the project to the tutorial library, that is fine with me.

Not elegant, but definitely easier:

https://we.tl/t-wUtvg27nwI

1 Like

Thanks, Gruk - I will look into it