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)…
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.
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.
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.
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!