I have a strange problem with an array and I just blank on what is wrong.
I have an array with several variables (bool) and I use a function to check the variables in the array if they are true or false.
array[ bool1, bool2, bool3…]
if array[0] == true then do thing.
The problem now is even so the bool variables are true (I checked) once I pull them from the array it says false.
I just don’t understand why? Is this a update problem? Do I need to refresh the array every time a bool changes? How do I do that?
Or is there a different problem I do not see.
Pls help, I feel like I do not see the forest because of all the trees.
I am not sure what exactly you mean by “Pull them” from the array. If you mean reading them, this test below shows it works perfectly fine.
extends Node2D
var boolean_array: Array = [
true,
false,
true,
]
func _ready():
for i in range(boolean_array.size()):
if boolean_array[i]:
print("Element %d is true" % i)
else:
print("Element %d is false" % i)
Element 0 is true
Element 1 is false
Element 2 is true
As mentioned by @matheusmdx, the error is somewhere else in your code.
Perhaps you meant when you pop the element from the array:
extends Node2D
var boolean_array: Array = [
true,
false,
true,
]
func _ready():
var index = 0
while boolean_array.size() > 0:
var value = boolean_array.pop_front()
if value:
print("Popped element %d is true" % index)
else:
print("Popped element %d is false" % index)
index += 1
print("Final array state:", boolean_array)
Still works perfectly fine.
Popped element 0 is true
Popped element 1 is false
Popped element 2 is true
Final array state:
Thanks for the help. Your comments fit my own expectations and I think the problem is the update of the boolean.
At the start they are all set to false but are changed along the way to true and I suspect that when I let my function test the array that it uses only the original false states and not the updated ones.
But until now I thought that the variable from the array is automatically updated when I array it. Does the array actually only safe the state of the variable? Or does it safe the variable name and then checks the actual content?
If I have to update the array every time I change one of the boolean, how can I do this most elegantly?
var bool1 = false
var array = [ bool1 ]
bool1 = true
print(array) # says [ false ]
Primitive types like bool, int, etc are copied. You are trying to create a reference to your bools, which isn’t possible like this. You could put them into a container class or array, but somehow I feel you should give a concrete example of what you’re trying to do and maybe there is an alternative solution for your problem.
This is exactly what is happening.
I used the array to check for the status of the bools inside of a “for” setup.
func _craft_ready():
for i in recipe_demand.size()/2:
if ready_array[i-1] == true:
craft_ready = true
else:
print(ready_array[i-1])
craft_ready = false
return
If this does not work with an array, I’m open for better solutions.
Thanks for all the help, I was able to solve the problem. I understand arrays better now I use the array directly instead of changing the variables. But there was another twist I did not see coming.
The i in the for loop did start with 0 instead of 1. Which caused it to check the last array position (I used i-1 to ensure I would start with the first position) which was always false in the tests.
I’m sure i normally starts with 1 and not 0 in such a for loop? I will test this in the future more.