Get Global Variable Property Dynamically

I have a global object and I would like to access a value based on a scene varaible.

For example, how do I access currentQuestionNumber of my global quiz.

I’d like the value of quiz.records[currentQuestionNumber].question.

These two methods come up empty.

This assignment works well at the beginning of the quiz when we assign the first question text at index 0.

But how do I use a scene variable as the index?

Please edit your posts instead of replying to yourself.
If you want to use a variable as index instead of the 0, write the variable inside square brackets like you did in your first screenshot.
https://wiki.gdevelop.io/gdevelop5/all-features/variables#accessing_child_variables_dynamically

My first screenshot didn’t work with square brackets. How would you change it to make it work?

Regarding replying to myself, I want to post 2 images. How am I supposed to include two images in my original post? It is not allowed for some reason.

“some reason” being the rules of the forum. Circumventing them is not the way to go. :slight_smile:

Please refer to the wiki page I linked for details on how dynamic access works.
I do not know your data structure, so I can’t provide any help.
Here’s an example from a personal project:
image
Notice the ToString() to convert a number to a string if necessary.

@Gruk I do appreciate the help, but rather than mock “some reason”, perhaps provide the reason and intuitively understand that I am inquiring about the reason. I know you are a stickler for the rules, but often more than 1 image can accompany a question making it easier to digest.

From your example it looks like your array indexes are numeric strings (unusual, less performant). I’ll try converting my index to a string.

Just to clarify, the forum does allow multiple images per post, but it might be an account age restriction.

For your question: If you’re using a structure variable, all structures names must be strings and cannot start with a number (the editor may allow it but the actual engine logic will not). If you’re using an array, all indexes must be numbers, meaning if you’re using a string variable you might need to convert it to a number instead.

If you’re using a mix of arrays and structures, both rules must be adhered to, which can be a bit of a pain. In that case I’d recommend just changing all of your arrays to structures, and make the children named “question1” 2 3 etc, and you can just do ToString(“question” + Variable(currentQuestionNumber)) to get the child dynamically.

Edit: to add another clarifier- When dynamically accessing a child variable of a structure variable in GDevelop, the dynamic path must be a string. So if you are accessing a number variable, you need convert it. For your screenshot, you’d do GlobalVariableString(quiz.records[ToString(Variable(currentQuestionNumber)].question)

but again, if your “records” child is a structure, it’s children cannot have a number as the first character in the name, it’ll just not process at all during preview/execution. I’ve tested with both arrays and structures, and so long as you adhere to this for structures it has worked both ways.

Here’s an example scene variable set up:

Here’s the event:

Here’s the result in the debugger:
image

To add some context as an ex-DevOps person myself, I understand switching terminology can take time, but just to clarify some Gdevelop engine/community specifics:

  • Objects are the visual items you add in the scene editor (Sprite Object, Tilemap Object, Text Object, etc)
  • Structure Variables are closer to what you’re used to when you refer to objects in programming languages, with children that can be numbers/values, strings, more structure variables, arrays, etc.
  • All number variables are floats in the backend, there’s no strict int/float/double typing, etc.

Some of this is carryover from a specific version of Javascript, which causes its own confusion. Keep in mind that while parts of the engine are Javascript, the language used within the game logic/editor are not. The biggest one to keep in mind terminology wise is there are no nodes, and objects mean things in the visual editor’s object list.

2 Likes

@Silver-Streak You’re the best! Thank you so much for the examples and detailed explanations in regards to everything. I have a much better understanding and I am eager to update my code and get the most out of this awesome platform. Thank you!

1 Like