#For loop error

1 messages · Page 1 of 1 (latest)

wanton rampart
#

I am trying to reset the three buttons (which are in the index called slot) using the for loop. I could just set the values individually but this got me curious what is wrong with it when I use the for loop

cold summit
#

pretty sure it's for i in len(slots)

#

also, that doesn't look like the best way to do it

wanton rampart
cold summit
#

you could, instead, add them all to a "buttons" group

wanton rampart
cold summit
#

and then do get_tree().call_group("buttons", "set_pressed", false)

#

it basically just does all that, but in one code line

#

just makes your code a little cleaner

wanton rampart
#

Thxxx will do that

cold summit
#

but for loops are still important to understand

#

you could also, instead of

for i in len(slots):
  slots[i].set_pressed(false)

do

for button in slots:
  button.set_pressed(false)

these both achieve the same result, in a different way

wanton rampart
#

Ahhhhh, didn't know that, looks like I've interchanged and combined this two different structure, thx for expanding

cold summit
#

wayt nvm it's not len(slots)

#

it's slots.size()

wanton rampart
#

what's the difference? the len(slots) still works.

cold summit
#

.size() looks cleaner and matches the godot docs

#

you could use either one tho

wanton rampart
#

Im calling this function in the parent of this node, and it would be best using the group method rather than referencing it then call the function, right? or nah?

cold summit
#

either one works, but the group method does make for cleaner and more readable code

wanton rampart
#

Okie thnx man

main helm