How do I Set a instance variable with an external file?

Just started using Construct 2? Post your questions here

Post » Fri Sep 11, 2015 4:43 am

How do I Set a instance variable with an external file?
I have this file
Code: Select all
{
       "damage": 2,
       "resistance": 16,
       "health": 1,
       "speed": 1,
       "acceleration": 20,
       "traillength": 0,
       "wavethreshold": 0,
   "homingthreshold": 0,
       "selfdestruct": 1,
   "armingtimer": 0,
       "explosiontype": -1;
}



and I need to load it and set it to the instance variables
B
13
S
6
G
1
Posts: 150
Reputation: 1,962

Post » Fri Sep 11, 2015 10:28 am

Where exactly have you stucked? You can load data for example using AJAX if it is a PHP output. Then parse output to string without "new line" characters and set it as a value of your text type instance variable.
ImageImageImage
B
28
S
17
G
82
Posts: 1,034
Reputation: 45,912

Post » Fri Sep 11, 2015 8:07 pm

I'm not sure what you mean by PHP output

I am using a .json file to do this
B
13
S
6
G
1
Posts: 150
Reputation: 1,962

Post » Wed Sep 23, 2015 3:42 am

Shadowblitz16 wrote:I'm not sure what you mean by PHP output

I am using a .json file to do this


Have you looked at Yann's Json plugin in the extending C2 section of the forum? I belive that would be worth looking into for you. However you can with a few adjustments change the json syntax to be used in a dictionary object.

Code: Select all
{"c2dictionary":true,"data":{
"damage":"2",
"resistance":"16",
"health":"1",
"speed":"1",
"acceleration":"20",
"traillength":"0",
"wavethreshold":"0",
"homingthreshold":"0",
"selfdestruct":"1",
"armingtimer":"0",
"explosiontype":"-1";
}


Then import that into your project files folder. Use AJAX to load project file then on success have your dictionary load that json from Ajax.LastData

In your game just call from that dictionary and change the keys according to any effect the player may have on them. Also cool thing is you can save that dictionary as a json and reverse the process to save your game.

Also when using files inside C2 best to leave them a .txt C2 will be able to tell the difference between formats like php, json, and sql :)
"Were all mad here. I'm mad, your mad." "How do you know I'm mad?" Asked Alice. "You must be" Said the Cheshire Cat. "Or you wouldn't have come here"-Lewis Carroll: Adventures In Wonderland
B
7
S
3
Posts: 113
Reputation: 1,295


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 2 guests