How do I store and retrieve level data?

0 favourites
  • 11 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • 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!

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

    Please help?

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

    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

  • Apologies for the self-bump, but can someone please have a look at my capx?

  • 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.

  • {
        "c2array": true,
        "size": [10, 1, 1],
        "data": [
            [
                ["Apple"]
            ],
            [
                ["Banana"]
            ],
            [
                ["Cherry"]
            ],
            [
                ["Pepper"]
            ],
            [
                ["Grapes"]
            ],
            [
                ["Pumpkin"]
            ],
            [
                ["Star Fruit"]
            ],
            [
                ["Strawberry"]
            ],
            [
                ["Tomato"]
            ],
            [
                ["Peach"]
            ]
        ]
    }[/code:28r9ycza]
    
    As you can see below, I tried both [b]Load from JSON string AJAX,LastData[/b] - and by using the tag - neither seems to work.
    
    [img="http://i445.photobucket.com/albums/qq178/blackcryptclan/ScreenShot_zps5f70e154.png"]
    
    Then after using Download as JSON data with filename "test.json" the file contains...no fruit names   
    
    [code:28r9ycza]{"c2array":true,"size":[10,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]]]}[/code:28r9ycza]
    
    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.
  • ...

    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.

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

  • > ...

    > 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!

    {"c2array":true,"size":[10,1,1],"data":[[["Apple"]],[["Banana"]],[["Cherry"]],[["Pepper"]],[["Grapes"]],[["Pumpkin"]],[["Star Fruit"]],[["Strawberry"]],[["Tomato"]],[["Peach"]]]}[/code:drdmpi7m]
    
    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.
  • How do I get data from a local file that has NOT previously been imported into the C2 project?

    I'm currently using :

    AJAX Request "Filename.json"

    Array Load from JSON string AJAX.LastData

    This only works for a file that has previously been imported to the project. It's useless.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)