Read Settings from File

Get help using Construct 2

Post » Tue Sep 11, 2012 10:44 am

The game has a lot of tunable parameters.

Currently these are hard coded in global variables, which is bad.

I was hoping the dictionary would have a load from file or similar.

Is there any way in C2 to read settings from a file at runtime, so that testers/designers can modify the parameters?

There dont seem to be any objects in C2 to read any resources (images, sprites, files, XML) from a URL.
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Tue Sep 11, 2012 10:47 am

B
42
S
9
G
6
Posts: 437
Reputation: 5,626

Post » Tue Sep 11, 2012 11:08 am

You can also load Array and Dictionary object data from JSON.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,498

Post » Tue Sep 11, 2012 11:34 am

Brilliant!
Thanks.
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Tue Sep 11, 2012 11:44 am

Sorry, does anyone know the format of the json text objects required to load into a dictionary? I need to create the file using notepad or similar with a bunch of key value pairs in can then load into the dictionary object or similar.

I know XML, but dont know json. is there a json eqiv. of the DTD of the format required to load in to a dictionary?nutmix2012-09-11 11:45:10
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Tue Sep 11, 2012 12:08 pm

OK, forget the last post - the dictionary object cant read json files - I thought the load action would load from file, but it only can load from hand entered text at design time.

It looks like the only route is write a some AJAX code.

Done this, and by luck you can use a relative URL which is just the name of your json file in quotes.

So it works - I can read in some dictionary settings via a file at runtime.

What is the earliest time I can do this? I need to get the entire game to wait till this has done, which is a pain.

Im using "On loading finished". Is there an eariler event I can use?

The format of the json file is:

{"c2dictionary":true,"data":{"test":3,"test2":2}}

The only problem I have now is that you cant edit the file in the single file project, you have to delete it and re-add it from somehwere else if you want to change the files contents. There is an "open" option if you right click on the file in the project, but this does nothing (for me anyway).
nutmix2012-09-11 12:33:11
B
15
S
5
G
2
Posts: 357
Reputation: 3,691

Post » Tue Sep 11, 2012 12:24 pm

[QUOTE=nutmix]OK, forget the last post - the dictionary object cant read json files - I thought the load action would load from file, but it only can load from hand entered text at design time.[/QUOTE]
But you can enter an expression returning a string, you don't have to type it out. For example you can AJAX request a JSON project file and load that. See Using project files in Construct 2 for some examples.

Note the JSON format used by Dictionary and Array is specific to C2 - export some JSON from empty objects and look at the result to see the format you need to use.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,498

Post » Tue Sep 11, 2012 12:43 pm

Ok, solved the not being able to open the file once imported issue - renamed it to .txt and now it opens in notepad.
B
15
S
5
G
2
Posts: 357
Reputation: 3,691


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 31 guests