How do I load the data from a JSON file into an array?

Get help using Construct 2

Post » Tue Jun 23, 2015 5:34 pm

I've been searching the forums for more than an hour for a solution to this seemingly simple problem but either I wasn't able to find it or the explanations I found were too complicated for me. I am new to arrays and I would like to know how I can load the data from my menu_data.json file into my empty menu_data array. I wrote the data inside the file via notepad in the exact format that construct saves JSON files. Here's what it contains:

Code: Select all
{"c2array":true,"size":[6,2,1],"data":[[["PreMenu"],[0]],[["StartMenu"],[0]],[["OptionsMenu"],[1]],[["CampaignMenu"],[1]],[["LevelMenu"],[3]],[["Game"],[4]]]}

I just want my game to load the data into the empty array when it starts. Please note that I need pretty specific instructions, I get the array concept but I'm still fuzzy on the commands. Also, I have no idea where I should place the file so the project can load it.
B
7
S
1
Posts: 29
Reputation: 483

Post » Tue Jun 23, 2015 5:37 pm

You need to use Ajax plugin to load up the file.
Ajax:Request project file and then Ajax:On completed set array to Ajax.LastData
ImageImageImageImage
B
158
S
67
G
43
Posts: 2,603
Reputation: 36,003

Post » Tue Jun 23, 2015 5:43 pm

shinkan wrote:You need to use Ajax plugin to load up the file.
Ajax:Request project file and then Ajax:On completed set array to Ajax.LastData

I tried that but the only files I can choose there are .pngs. I found the folder where they are located and placed by .json file there but I still can't select it in the top-down list. Maybe I got the wrong folder. The only folder I found that contained the files in the list was Program Files\Construct 2\exporters\html5.
B
7
S
1
Posts: 29
Reputation: 483

Post » Tue Jun 23, 2015 5:58 pm

Image

In your C2 project right click on the Files folder and choose "Import file" and from here select and import your json file.
ImageImageImageImage
B
158
S
67
G
43
Posts: 2,603
Reputation: 36,003

Post » Tue Jun 23, 2015 6:14 pm

shinkan wrote:Image

In your C2 project right click on the Files folder and choose "Import file" and from here select and import your json file.


Right, I knew I was missing something simple. Thank you. However I did this and nothing's happening, my array is still empty:

Image

Do you mean something else by "set array to Ajax.LastData"?
B
7
S
1
Posts: 29
Reputation: 483

Post » Tue Jun 23, 2015 6:50 pm

Right, I knew I was missing something simple. Thank you. However I did this and nothing's happening, my array is still empty:

Image

Do you mean something else by "set array to Ajax.LastData"?


I tried it with a different array and .json file and it definitely worked. I'll figure out why the first one doesn't work. Thanks for clearing things up, now I can handle data much more efficiently.
B
7
S
1
Posts: 29
Reputation: 483


Return to How do I....?

Who is online

Users browsing this forum: FabianB, Seruza and 7 guests