Hi.
I’m trying to access the value of a Boolean variable in an object array, but the Gdevelop UI won’t let me choose to “use the variable as Boolean” and choose between true/false. Instead it considers the array variable as a text variable by default, and I don’t have the option to change it. As a result, the engine won’t read the condition at all.
Below is a screenshot of the object variables that are involved to provide more context. What is supposed to happen is that the game will check the value of PlayerManager.WeaponList[PlayerManager.WeaponIndex+1] to see if the weapon at index PlayerManager.WeaponIndex is unlocked or not (true/false), which is used to tell if the player can equip it or not. The player can cycle through weapons with an input that adds +2 to PlayerManager.WeaponIndex.
I still manage to obtain the correct value of the Boolean variable inside the array when using the exact same expression in a text object. PlayerManager.WeaponList[PlayerManager.WeaponIndex+1]
The text object will show : “Weapon Unlocked : true” or “false” depending on the initial value I input in the object array.
I could be doing something wrong when writing the expression in the object variable condition event. But since it works correctly when I write the same expression in a text object, this makes me think that it could be a UI bug in the engine as well. So if I don’t get a solution here after some time and have to use a different approach, I’ll probably make a post about it in the bug report section.
The problem was actually caused by a “Or” condition. Apparently, the “Or” condition can deny object picking under certain circumstances - which is very counter-intuitive, hence it took me a while to figure the actual cause of the problem.
I don’t have the permission to delete the original post, but I think a moderator should delete it to avoid confusion.
Structures can have children of any type but if they’re in an array then the children need to all be the same type as they were initially declared.
You could have an array of players with children named team and score. If team is a text and score is a number then they need to be that type for each index. If setup at design time, the easiest way is to copy and past the structure.
That doesn’t seem to be what @Bubble2 is doing though. They just have an array with both text and boolean elements, which I thought was OK (even if potentially confusing)
For example say I want to save a bunch of objects’ color and position, it seems more convenient to just save the full color text since that is the format I want to be reading it back as. So I would have a bunch of 3-element arrays with color as text, then X and Y as number. Is there a reason not to do this other than having to be careful about mismatching types?
When adding events, GD picks the variable type based on how it’s declared. That’s easy for a simple variable. You create a number variable and it gives you the actions for a number like adding a number to it.
Text on the other hand would add the character to the variable instead of mathematically.
It’s the difference between
1+1=2 and “1” +“1” = “11”
The issue with arrays is that GD expects the format that matches the action or condition that you used when you added the events. It doesn’t adjust to the current indexes variable type. If you try to use a different type, it will most likely fail and return zero.
As for simplicity. I would rather have a structure instead of an array where it’s color, X, Y.
I prefer
Create object at data[index].X, data[index].Y
Set Tint of object to data[index].color
index plus 1
Over
Create object at data[Index], data[index+1]
Set Tint of object to data[jndex+2]
index plus 3
(You could use for each child for the structure version , I was just trying to keep it easy to read and compare )
You could use an array of text but you would need to use ToNumber(data[Index]) for each number.
How the values of the data are set depends if the values are set during design time or at runtime.
There are ways to simplify things. If you use the array tools extension you could create an array of text with the value seperated by commas. This would work because the array is all text.
Yeah I understand that, and can see why it would be your preference, I was asking if there’s any other reason to avoid mixed arrays. My preference is to just use arrays unless I am actually getting some other use out of the named indices, and I’m accustomed to “weak-typed” programming anyway.
@Bubble2 sorry to get so off-topic. Your original usage of mixed-type is pretty confusing. You could just have an array or structure full of booleans:
WeaponList:
0 = TRUE
1 = FALSE
2 = FALSE
The above would indicate that you have weapon 0 but not 1 or 2. To test for weapon A use condition “boolean variable” pointing to WeaponList[A]
(A being a variable number ofc)
Gdevelop is stricter now. It used to be looser with variables when you had to use Variable() and VariableString(). Now, you need to declare them and then it restricts the actions and conditions to that type.
It would wouldn’t be as difficult to use a mixed array if done at runtime but at design time, it seems like you would have to really work to make sure everything is in sequence. One missing value could throw everything off.
Structures might require some extra work but for me, it’s much easier to read and figure what each variable does. Especially, weeks or months later.