(SOLVED)JSON Resource Loading Help

Hello, I have been trying to read off a JSON resource in GDevelop and have been having some problems.
This is the JSON I have been using to test loading a JSON resource into a variable:
{
“name”: “TestVariable”,
“type”: “string”,
“value”: “Yes”
}

I tried using the JSON Resource Loading extension but when I look in the debugger instead of reading the JSON as is like above it reads it in a weird way where it puts everything into a structure.
Here is a image of what it is doing:

I’ve been looking for a while and still haven’t found a fix or an alternative to this so any help would be greatly appreciated.

The action loads and converts a string into an existing variable (here, TestVariable), which makes the existing variable a structure. The names and type of the variables you load into your structure depend on how you formatted the JSON string, so something like
{“Text”: “Yes”, “Number”: 0, “Boolean”: false} becomes

You might need the Load a text from a file, which will load a text (here “Yes”) and put it into a pre-existing variable you select.

Oops, my bad, I just saw you mention you were using an extension. I thought you were using the regular actions in GDevelop.

2 Likes

I thought about using the load text from file action but I am targeting mostly for mobile and HTML and those don’t work with that which is why I am trying to use the JSON as a project resource instead.
Also I tried testing out the JSON string you provided using the convert JSON string built into the engine and I couldn’t get it to reproduce what you got in the debugger. Even if it did work is there anyway to make it so instead of writing the variable out into a structure that it could instead just overwrite the existing variable normally?

Yes, probably more ways than I can think of.

You can do an action like
image
and put it after whatever condition must be met to get TestVariable to have Yes as the value.

But probably I’d better ask: What type of variable are you overwriting? What are you wanting to overwrite it with? This way you may receive more precise help.

2 Likes

I’m making a game with trivia elements so I’d be editing multiple variables for things such as the questions and answers. I already have many of the levels put into excel sheets for when I was using Unreal like this one: TutorialDataTableOld - Google Sheets

Unreal allowed devs to upload data tables like this but I have been recently wanting to switch to GDevelop although I understand that GDevelop doesn’t support importing excel sheets which is why I have been trying to use JSON resources instead.

The action you showed would work but it would be extremely tedious trying to make all the questions and levels and in the future I would also like to allow users to make their own trivia levels which is why I would like to just have a JSON resource for each trivia level and just be able to read off that for the variables during gameplay.

Here’s an example of the variables I want to change:
{
“name”: “Tutorial”,
“type”: “array”,
“children”: [
{
“type”: “structure”,
“children”: [
{
“folded”: true,
“name”: “Answers”,
“type”: “array”,
“children”: [
{
“type”: “string”,
“value”: “”
},
{
“type”: “string”,
“value”: “”
},
{
“type”: “string”,
“value”: “”
},
{
“type”: “string”,
“value”: “”
}
]
},
{
“name”: “CorrectAnswer”,
“type”: “number”,
“value”: 0
},
{
“name”: “Question”,
“type”: “string”,
“value”: “”
}
]
}
]
}

And be able to change it to this:

{
“name”: “Tutorial”,
“type”: “array”,
“children”: [
{
“type”: “structure”,
“children”: [
{
“folded”: true,
“name”: “Answers”,
“type”: “array”,
“children”: [
{
“type”: “string”,
“value”: “Calf”
},
{
“type”: “string”,
“value”: “Cub”
},
{
“type”: “string”,
“value”: “Puppy”
},
{
“type”: “string”,
“value”: “Kitten”
}
]
},
{
“name”: “CorrectAnswer”,
“type”: “number”,
“value”: 3
},
{
“name”: “Question”,
“type”: “string”,
“value”: “What do you call a baby dog?”
}
]
}
]
}

1 Like

Ah. Tedium. The scourge of intelligent people.

Try this test with Yarn, because I have not used that extension you mentioned earlier. But you mention my JSON does not work in it.

Make an array variable named Tutorial, assign it one child.

Add some beg scene events.

When picking the action to load dialogue from a JSON file hit the Edit with Yarn button.

Double click the default title of the page that opens, name it Tutorial. Double click the node named Start and paste these 4 lines in there:

[
{"Answers":["Calf","Cub","Puppy","Kitten"],"CorrectAnswer":4,"Question":"What do you call a baby dog","folded":true},
{"Answers":["Honey","Syrup","Sugar","Butter"],"CorrectAnswer":1,"Question":"What do bees make?","folded":true}
]

Save Yarn thing. Run project with debugger. Check if your array Tutorial looks something like this

If so then maybe there is error in the extension you are using??

ps You can add more white spaces if you like.

2 Likes

Thank you, this works great for what I’m trying to do. I think you are right that there was an error with the extension I was using.
I appreciate all your help!

2 Likes

The only things I would add are, since the test is over you might want to give the array Tutorial a structure child with the actual variable names and types you will be using and some default values. It will be overwritten of course but should help prevent anything problematic from happening because of not explicitly declaring them. Example

And of course I should have changed CorrectAnswer to 3 and 0 because: array.

2 Likes

Alright, it was working without it but I’ll add it just incase to prevent any problems like you said. Thanks!

2 Likes