How do I store and retrieve level data?

Get help using Construct 2

Post » Sat Jan 31, 2015 10:17 pm

I want to save and load level data - preferably as separate files. How do I do this.

The data will be grid based - each entry in the grid will represent a location on the layout (x and y in the grid). The status of that location (what is there and it's state) would be stored in the grid.

I've looked at webstorage, json and arrays and this seems to be a way to do what I want, but the semi-temporary nature of webstorage worries me and that I can't see a way to access several files (levels) individually - ultimately the levels would be retrieved (from a server) as needed in batches (level packs).

I sort of understand that I can store an array as a json string - I don't know how I would get that string to a file and how I would retrieve it later.

Here's a description of the sort of information that'll be saved (per level)...

A grid (currently 13x13 but may change) with each entry containing a string which will 'describe' what is in that location - My program will parse the string to recreate the screen (layout) on load.

tileset, tile number, health - would be represented by a series of characters eg "001 012 100" - would mean tileset 1, tile 12 and 100% health.

I hope I have explained clearly enough for you to understand what I want to do.

Thanks for reading!
Last edited by blackcrypt on Tue Feb 10, 2015 8:57 pm, edited 2 times in total.
Image
B
14
S
3
G
1
Posts: 61
Reputation: 1,003

Post » Mon Feb 02, 2015 2:53 am

I can't believe that there are no means to save and load level data into a construct program!

Please help?
Image
B
14
S
3
G
1
Posts: 61
Reputation: 1,003

Post » Mon Feb 02, 2015 3:08 am

i think you san save your desired files inside an array, then save that array inside webstorage with "set local value" action.
then at start of layout, check whether that array was previously saved or not with "Local key exists" condition.
if exists, load it as JSON file.
"If you want to move a mountain tomorrow, you should start by lifting stones today."
B
62
S
14
G
2
Posts: 287
Reputation: 6,608

Post » Thu Feb 05, 2015 5:53 am

I'm still having trouble working out how to load / save game data locally - not the game state - just bits and pieces of data such as level data.

I cannot believe this essential requirement of modern games is such a chore!

OK, I made a demo program to help you work out where I am falling down. Please remember this is NOT a game - it's just an example program to get multiple sheets of data from an array to my hd and back again.

Image

In this example there is no way to place fruits on the sheet - I'm currently only worried about the FruitNames array - I think I am doing the right things to load the array from a json file - but the fruit names do not appear where they are supposed to - I just get a "0" instead.

Anyway, If someone could look at my capx and maybe fix it up so the selected fruit name appears above the pallete panel (on the right). The fruit name must have been retrieved from an array that was previously saved to disk.

Note : Pressing 'S' or clicking on the save icon will 'download' the FruitNames array to test.json. I did that to test whether the array is getting the names from FruitNames.json. It isn't :cry:
You do not have the required permissions to view the files attached to this post.
Image
B
14
S
3
G
1
Posts: 61
Reputation: 1,003

Post » Sat Feb 07, 2015 12:30 am

Apologies for the self-bump, but can someone please have a look at my capx?
Image
B
14
S
3
G
1
Posts: 61
Reputation: 1,003

Post » Sat Feb 07, 2015 1:02 am

Use Load from JSON string AJAX,LastData to load the json file into your array.
And you need to fix your json file. You are missing quotation marks between your fruit names.
It will work once you fix those two things.

Also as a note you could do your sheet changing code as below to eliminate having to reset the variable each time.
You do not have the required permissions to view the files attached to this post.
B
33
S
8
G
2
Posts: 100
Reputation: 2,635

Post » Sun Feb 08, 2015 12:08 am

Code: Select all
{
    "c2array": true,
    "size": [10, 1, 1],
    "data": [
        [
            ["Apple"]
        ],
        [
            ["Banana"]
        ],
        [
            ["Cherry"]
        ],
        [
            ["Pepper"]
        ],
        [
            ["Grapes"]
        ],
        [
            ["Pumpkin"]
        ],
        [
            ["Star Fruit"]
        ],
        [
            ["Strawberry"]
        ],
        [
            ["Tomato"]
        ],
        [
            ["Peach"]
        ]
    ]
}


As you can see below, I tried both Load from JSON string AJAX,LastData - and by using the tag - neither seems to work.

Image

Then after using Download as JSON data with filename "test.json" the file contains...no fruit names :cry:

Code: Select all
{"c2array":true,"size":[10,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]]]}


Something is not working and I don't know why - I've done everything I know.

This is why I suggested someone might like to download the capx and fix the issue so I can see what I did wrong.
Image
B
14
S
3
G
1
Posts: 61
Reputation: 1,003

Post » Sun Feb 08, 2015 2:46 am

thatserafimkid wrote:...
And you need to fix your json file. You are missing quotation marks between your fruit names.
It will work once you fix those two things.

...

I already told you why it's not working. You can't expect people to just fix your issues for you. You need to learn why there's an issue and be able to correct it.

Find your json file inside your project open it in notepad and add the missing quotation marks.
B
33
S
8
G
2
Posts: 100
Reputation: 2,635

Post » Sun Feb 08, 2015 3:36 am

I had the same problem a while ago and here you can find the solution, hope this helps.

how-do-i-load-data-from-json-string_t119830
B
46
S
9
G
5
Posts: 110
Reputation: 3,782

Post » Sun Feb 08, 2015 5:03 am

thatserafimkid wrote:
thatserafimkid wrote:...
And you need to fix your json file. You are missing quotation marks between your fruit names.
It will work once you fix those two things.

...

I already told you why it's not working. You can't expect people to just fix your issues for you. You need to learn why there's an issue and be able to correct it.

Find your json file inside your project open it in notepad and add the missing quotation marks.


Yes, I already did that - You'll notice that my json file is included at the top of my last post. I can expect people on a community forum to help and sometimes just look at the 'code' and fix it so I can see what's wrong at my end. Those that don't want to do that can opt to NOT reply. Perhaps you fall into the latter group?

I have also minified the FruitNames.json file contents to be extra sure!

Code: Select all
{"c2array":true,"size":[10,1,1],"data":[[["Apple"]],[["Banana"]],[["Cherry"]],[["Pepper"]],[["Grapes"]],[["Pumpkin"]],[["Star Fruit"]],[["Strawberry"]],[["Tomato"]],[["Peach"]]]}


So, as it stands, I have done everything that I think I should have done, but the fruitnames still do not appear - the array is empty.

[EDIT] Problem solved - and it was simply a case of re-importing my FruitNames.json file into the project. I assumed (wrongly) that the file was retrieved at runtime from disk.

[EDIT] I have also noticed that beautfied json does not work in C2.
Image
B
14
S
3
G
1
Posts: 61
Reputation: 1,003

Next

Return to How do I....?

Who is online

Users browsing this forum: mindragon73, nahima, tdcostas and 20 guests