[Solved] Extract a specific child from a random structure

Hello everyone, how exciting, it’s my first post in this community (and can I say that GDevelop and the work you do here are a really cool? There, I said it).

How do I… extract a specific child from a random structure?

I have loaded a series of texts from a JSON into the “Action” variable and I would like to randomly select the “id” (0,1,2,3,4) to display the corresponding text “ph1txt” within a text object (ph1text).
I know for sure that the JSON is properly formatted (if I manually select the specific child, I can display the correct text), but I have searched online and I haven’t found anything specific about it (even from the Quiz Game Template from which I obtained a couple of events, it doesn’t seem to cover this scenario)…

What is the actual result

With these Events, instead of the correct text I have a zero. What am I missing?

1 Like

Hi @jordi82 welcome to the community!

There are few errors in your expressions.

  • Change variable RandomID: Random(VariableChildCount(Action.id))
    ^
    Since you used RandomInRange from 1, you dropped out the first child [0]
    Also you added Action twice, so you’re actually getting Action.Action.id

  • Change text “ph1text” to: VariableString(Action.id[Variable(RandomlD)].ph1txt)
    ^
    I’m not sure why you used VariableString(actionid)

Note: You should use Variable(RandomID) instead of VariableString(RandomID) since the it’s a number variable and not a string. Also the variable “id” can be an array instead of a structure, I think it would be easier for you to manage.

Hope this helps.

Hi insein, first of all thank you for your fast reply!

You’re right, there were few errors. I started with GDevelop a week ago and I feel pretty stupid now, I was in confusion, this part about structures and children is pretty complicated, for now.

I inserted all your corrections (Variable(RandomID) and “id” as array too), but I have the same exact problem as before (the 0 instead of the text).

What could be the problem?

As I said, the json file works, infact if I use
Change text “ph1text” to: VariableString(Action.2.ph1txt)
it shows the right text.

No problem!

Thanks for sharing screenshot of the JSON, the problem is clear now. The JSON is different than the scene variable you shared, and the expressions I sent are based on the screenshot of the scene variable. That’s why it didn’t work.

This is how the JSON would look like in GDevelop:

Based on the JSON, The “id” is a child of the “Action” array just like the other strings. So the correct expressions would be:

  • Change variable RandomID: Random(VariableChildCount(Action))
  • Change text “ph1text” to: VariableString(Action[Variable(RandomlD)].ph1txt)

You’re a lifesaver, insein!

Okay, so I thought I understood JSON, but apparently not…

Now I have RandomID as number, Action as array like in the image and the corrected events, but the random text selection doesn’t work; only the text from the first child always appears (“You prepare to receive a low filtering ball.”).

The first time I knew that something was wrong with the expressione, the second one I had a doubt about the JSon, but now I don’t even know where to look…

Thank you in advance, I’m learning even just looking at your corrections!


It’s because you’re getting the “ph1txt” string.

So you want to choose a random string from the random child of the Action array?

I mean that in ph1text appears only the Action[0].ph1txt, but I want to be chosen randomly from
Action[1].ph1txt
Action[2].ph1txt
Action[3].ph1txt
etc

where ph1txt is fixed and [n] is random.
I hope I’m being clear.

I tried this in my example and it worked fine for me, it gets a new text every time. I’m not sure why it’s not the case for you.

Note: I forgot to add the -1 to the random expression before, sorry about that (but it’s not related to your current issue)

Okay, I’m very tired:
in
VariableString(Action[Variable(RandomlD)].ph1txt)
there was a lowercase L instead of a capital I…

Now it’s perfect, thank you again for your time!

1 Like