Iterative examination of global variable values by means of sprite animation name

Greetings from Thessaloniki, Greece.

This is actually my first post in the forum. I am a new member of the community.

My question is this:

I am developing an educational game where the student visits 8 countries to gather some data. Everything starts and finishes in an introductory scene where the student chooses in which country to go. When he finishes, he returns to that scene.

I am trying to figure out a way to disable countries visited in previous round with some sort of iteration. Could do that with a bunch of if’s but I’ve come to understand that GDevelop is capable of more than that.

My initial idea was to come up with 8 global variables with value 0 (not visited) which I will change to 1 when the student is done with the country. In a similar vein, I have sprites for country names and flags whose animations have the same name as the global variables.

What I was going for is the following in pseudocode:

At the beginning of the scene
for each of the 8 global variables {
for each of the 8 flag animations {
if (flag animation name == global variable name) && (global variable value == 1) {
show that flag animation at x, y
move x a bit to prepare for next flag
disable country name from options
}
}
}

Is that sth that can be done? Search global variable names by sprite animation names and/or vice versa?

It would require some sort of compare condition which utilizes getters which I don’t know if it is possible or how to code it

if GlobalVariable(Sprite.getAnimationNameAsString()) equals 1

Any help or workaround would be much appreciated

1 Like

You can dynamically create variable structures in GD, and you can dynamically set the name of nested variables using square brackets (everything between square brackets is considered a string).

Example:

Events window:

t01

Results as it looks in the debugger:

t02

Knowing that, you can iterate trough your country objects at the start of the scene, asign each object an unique identifier and create a data structure with data slots for each country.

Example:

Events window:

Results as it looks in the debugger:

t04

So, each time you want to know if a country was visited (or modify that tag), you just have to look at the id object variable of the country and then look at that slot in the data structure.

For example:

In this example, if you click in a country that was not previously visited it will change the background to a random color.


Note that it is NOT necessary to create and iterate through these data structures to find out which country has been visited, since you can save that data in object variables for each country. But if what you want is to consult that data after restarting the scene you can apply the technique that I’m suggesting and save the data structure as a global variable.

Another possibility is to manually assign country names on object variables, and use those variables instead of numeric identifiers when creating the data structure.

3 Likes

Wow @erdo! Isn’t this an awesome welcoming to the community or what? Much appreciated.

The square brackets syntax was a great plus, didn’t know that. The remaining implementation is also excellent, though it will require me to overhaul my current logic.

If I could use a method inside the square brackets or do sth like “For each child variable of global/scene variable x” or “for each animation of object y” that would be awesome. Can I?

Just to shed some light, if you are willing to go the extra mile:

The scene is this (I said educational module, but it doesn’t mean it shouldn’t be fun to use!).

You match destination to capital and the corresponding map appears where you have to drag an x mark to the location of the country. Moving up and down simply changes the respective animation.

I used destination and capital objects with corresponding animations names, so as to compare equality (if destination.animationName() == capital.animationName() proceed). The same with flags, as you can see on the right. So, in order to utilize this setting and combine it with your input, I made this global data structure “countries” with child variables named after the animation names (countries.gr = 0).

But then again, I would need something like:

repeat for each child variable of countries
if destination.animationName([countries.childVariableName()]) == 1

or

repeat for each animation of destination
if countries[destination.animationName()] == 1

Is one of these two possible?

Even if I replicated the global data structure to the scene and update it every time the user returns there, I would also need to invoke the animationName() method inside square brackets.

I’ll play around with it, see if I ditched string names and used integers in the object animations and the countries data structure would do the trick.

Thank you again for your response, mate!

GDevelop have

  • An event to check the object’s current animation by its name
  • An action to set the object’s current animation by its name
  • An expression to get the object’s current animation as a string: object.AnimationName()

You can even use the animations names to build the data structure.

But there isn’t a native expression to get the number of animations of the object (although it is possible to obtain this data using javascript). A quick solution is to manually set a variable containing the number of animations of the object (but if you modify the numbers of animations of the object, you have to manually update the variable in your events, so it’s a functional but not recursive implementation).

Thank you for your prompt response @erdo.

I haven’t found the time to work on the game, but when I do, I will most certainly start from what you have suggested thus far.

There’s pure gold in there.

Thanks again. Will let you know how it worked.