2
\$\begingroup\$

I would like to know how to parse a JSON object and parse each value into either an array, or something that I can use to set values of several global variables in my project file. I want to avoid editing the Javascript file itself, so please try to provide an approach that can be done in Construct itself rather than an external way.

Code sample

 axonify.game = {
  gameData: function() {
    data = {
      name: 'Block Mania',
      timeLimit: 30,
      addedTimePerQuestion: 0,
      addedTimePerCorrectQuestion: 15,
      questionCount: questionCount
    };
    return data;
  },
\$\endgroup\$
  • \$\begingroup\$ Where do you want the data to reside? Construct offers file loading via http or local file access. Also, which file do you want to avoid editing, the data loader code or the data file? \$\endgroup\$ – Seth Battin Apr 24 '14 at 15:24
  • \$\begingroup\$ Data file such as the API of the Axonify javascript file that Construct 2 loads. I don't really need this anymore, but it would be nice for future projects to be answered nonetheless. \$\endgroup\$ – Helladen Apr 24 '14 at 22:38
  • \$\begingroup\$ You didn't explain what exactly you are trying to avoid editing, but I answered the part about doing it "the Construct way". \$\endgroup\$ – Seth Battin Apr 25 '14 at 0:27
1
\$\begingroup\$

The manual is pretty explicit about this.

Any external files can also be imported to your project via the Project Bar. This is useful for including any other resources your project might need, such as videos, additional images, JSON data, XML data, text data, documents, or other general files you might want to let the user download.

For ideas on uses for project files and possible workflows, see the tutorial Using project files in Construct 2.

That link within the manual does indeed point to useful method to utilize this feature (it explains later that json may be used in place of xml):

Loading XML data
If you want to read an XML file stored with the project, import the XML file as a project file. Then you can use the AJAX object's Request project file action to download the file, and then load the result in to the XML object. The events below show how this can be done. enter image description here

The further along in the article, the section Making level editors explains how this loading can be utilized to insert data that is edited separately from the project, allowing Construct2 games to be data-driven.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.