Saving characters' variable values to a single file?

  • Hi there. So I have a character who has multiple variables (eg Level, Type, Name, etc), and I would like to write them to a file, so that the game can re-read them and load the variables to the same character when it's loaded afterwards, preferably in a text file instead of web/local-storage.

    How should I do this?

    What I had tried to find beforehand:

    1) Savegame property. While this would definitely save the character, it would save the entire game & not just the specific character(s).

    2) XMLs. While it could be read to fill the specific variables, I haven't found a way to write to the XML files directly, or even selecting the specific tags.

    3) Individual text files for each variable. While this could definitely be used to store each variable, the task is tedious & requires each variable per file.

    Any help is appreciated. Thanks in advance!

  • There is a plugin called FileSaver, but it's very old, may no longer work.

  • Before I suggest the next thing, have you looked into combining a json or xml file using local storage?

    You could try a pure text file and tokenat as explained below.

    I'll break down the usage of tokenat for you, also so you can find out how to use it:

    tokenat(AJAX.LastData, 3, "|")[/code:2pmulfz2]
    
    I pulled the following line above from my gamekit, where I utilize it for setting the contents of inventory slots for key items like a bow or boomerang, as an example in this.
    
    I'm using AJAX here to pull from a project file (config.txt) but you can use other methods if you can think of it, but you can use AJAX to read directly from a config file if you have AJAX request/download the file.
    
    The number here (3) is the index it's asking for, to search for what contents to pull from. Keep in mind that the index is zero based, so if my available contents are "boomerang|bow|hookshot|bombs|lamp", 3 will return "bombs".
    
    The last value to note of ("|") is a separator that tokenat will use to distinguish the parts of a query, such as a comma or slash mark.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Nwjs?

    Just write to file Sprite.AsJSON

    And NWjs.ReadFile(NWjs.AppFolderURL)

  • Thanks for the reply, LaDestitute!

    So, if I understand you correctly, if I have a save file that looks like this, with each block representing level | power | money:

    5 | 7 | 12[/code:2413trgt]
    
    If I want to set my character's level, would I do this?:
    
    [code:2413trgt]character.power = tokenat(savefile.format, 2, " | ")[/code:2413trgt]
    
    And, respectively, to obtain the character's money in inventory, would I do this?:
    
    [code:2413trgt]character.money= tokenat(savefile.format, 3, " | ")[/code:2413trgt]
  • Close, but as I said, keep mind that the index is zero-based, so it counts up from zero. Also, you should format your config/data file with no spaces, thus like 5/7/12.

    So, level would be 0, and money would be 2.

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