#Checking if a variable is true in every single node of a group
1 messages · Page 1 of 1 (latest)
I'm trying to see if every node in the group "generators" has the bool working set to true. I would just use generators without breaking it down but it gives me an error about generators being an array.
ah yes, you are getting all nodes in that group. and that does return an array. by using the loop
for x in y
x will always be a single element out of that array, one node.
and each of those nodes will be considered once. in your case, you are setting the bool to true right when you hit the first generator that is working
so, to make this work like the other post said, you will have to start with true, then consider each generator, and set it to false if one is not working. if all are working, then the variable will stay true
be careful though, you are doing this every frame of your game, 60 times per second. that might not be good for performance
no, you need to remove the else
after the loop the variable will have remained true of no generator was off
though, if you want to be fancy, there is a different way to do this.
the array class has a method all(), which will check if all things fulfill a condition.
https://docs.godotengine.org/en/stable/classes/class_array.html#class-array-method-all
you could do
all_generators_on = generatiors.all(is_working)
func is_working(generator):
return generator.working == true
and if that is clear to you, you can even remove the == true, since working is a boolean already and it will return true or false
return generator.working
Still not doing anything.
how are you using it?
Nvm, I just needed a quick break and I got it.
Take a break or the break will take you
Hello! maybe you want to do something like this?
// Checking all the generators in the list 'generators'
for nodes in generators:
// Checking if the current gen of the list isn't powered
if (nodes.working == false):
// If the gen are off then updates de var
all_generators_on = false
// Stops the loop (for loop)
break
// Updates the var if the current gen is powered
all_generators_on = true