Check if a value is present an array

How do I…

Check if a value is present an array

What is the expected result

For instance, being able to tell if the value “1” is present in a global array variable.

For the context, I have a game with several levels and I want a trophy to be displayed next to each button when the corresponding level is completed. Currently it works, but my solution is ugly. It’s “game jam quality code”, it’s unthinkable for me to use my current method for more than a few levels.

What is the actual result

No child seems to be detected in my array when I use the instruction For each child variable of a structure or array.

Related screenshots

PS : the rule that says new users can only embed one media in their post hurts both the author and the readers of the post, as it prevents the author from using as many images as necessary to make their point clear :slightly_frowning_face:

It seems that some of this can be simplified with dynamic access.

Example:
For each Child of Trophy
If Variable(Completion[Trophy.Variable(ID)] is true…show Trophy.

Completion is a array or structure name
[Trophy.Variable(ID)] takes the value of the specific trophy instance and then it will show that specific trophy instance.

Example:
If Variable(Completion[CurrentLevelNumber]) is False
Then then set Variable(Completion[CurrentLevelNumber]) to True.

Completion is a array or structure name
[CurrentLevelNumber] takes the Variable(CurrentLevelNumber) current value (1,2,3…etc).
And then it checks the array/structure children that have the value as a Name*

So…If Variable(CurrentLevelNumber) = 2
It will look in the Array/Structure “Completion” for a child named “2”

This is just my first couple of thoughts though. You may need to fiddle and rework some things in order to fit this in/or modify some of the expressions/variables.
But this is what I would suggest to do.

1 Like

I don’t think this behaves you expect it.

Say for example, currentLevelNumber is 5, and completedLevels is empty. The appends command adds the value of currentlevelNumber to the array completedLevels at the end of the array, and in the case of an empty array, this will be index 0. It doesn’t put it in array position 5.

If you were to run the same append action with currentLevelNumer = 5 and the array now with 1 element, completedLevels would have 2 elements, indexes of 0 & 1, both indexes value 5.

Think of an array like a stack, where the bottom most item is index 0. When you append an item, it places the item at the top of the stack. The value of the item doesn’t determine it’s position in the array - that’s set by how many items are already in the array.


From the looks of things, what you want instead is to use a structure. This has a named key-value pairing. That means you can define the key value, unlike an array which sets the key to the next available index.

The events would then look like:


And with this:

You’d want to use the child name (aka the structure key) - this will be the levelNumber. The child is the value, and in my example, true or false.

2 Likes

Sorry for answering so late, and thanks a lot for your help H0ndredEx! Using the dynamic variables extension did the trick for me, it’s very powerful and works fantastic.

Thanks for your most useful answer MrMen! I didn’t know you could add a key-value pair using brackets in GDevelop and thought I couldn’t solve this without dynamic variables. I was focused so much on the dot syntax (completedLevels.lv1) that it didn’t even cross my mind that I could just use standard dictionary syntax like completedLevels[“lv” + VariableString(index)] …

I created a small test project to try this and can confirm your solution works perfectly.