[Solved]Saving values of variables to file

yes, this topic again… I belive it makes everyone tired. may be you should do the wiki properly…
there is a lots of topic in this forum about this, but lots of them are outdated and/or says different things. this is confusing.
so, what is the solution in 2019 Dec.?

1 Like
  • None on the browser without JS
  • None on mobile without using is
  • Using the Filesystem extension on desktop

For JavaScript solutions:

  1. Desktop through JS:
    Import the node fs module like you normally would and use it as always (remember the relative paths are different in the preview)

  2. Mobile through JS:
    Search on Google for filesystem Cordova. Open the link of the Cordova website with the documentation. See how to use it. Use it like described (WON’T WORK IN PREVIEWS).

Example Js usage:

Writing:

var type = window.TEMPORARY;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)

function successCallback(fs) {
   fs.root.getFile('log.txt', {create: true}, function(fileEntry) {

      fileEntry.createWriter(function(fileWriter) {
         fileWriter.onwriteend = function(e) {
            alert('Write completed.');

         };

         fileWriter.onerror = function(e) {
            alert('Write failed: ' + e.toString());
         };

         var blob = new Blob([runtimeScene.getVariables().get("hello").getAsString()], {type: 'text/plain'});
         fileWriter.write(blob);
      }, errorCallback);
   }, errorCallback);
}

function errorCallback(error) {
   alert("ERROR: " + error.code)
}

Reading:

var type = window.PERSISTENT;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
   fs.root.getFile('log.txt', {}, function(fileEntry) {

      fileEntry.file(function(file) {
         var reader = new FileReader();

         reader.onloadend = function(e) { 
            runtimeScene.getVariables()
            .get("hello")
            .setString(this.result)
         };
         reader.readAsText(file);
      }, errorCallback);
   }, errorCallback);
}

function errorCallback(error) {
   alert("ERROR: " + error.code)
   }

Deleting:

var type = window.PERSISTENT;
   var size = 5*1024*1024;
   window.requestFileSystem(type, size, successCallback, errorCallback)

function successCallback(fs) {
   fs.root.getFile('log.txt', {create: false}, function(fileEntry) {

         fileEntry.remove(function() {
            alert('File removed.');
      }, errorCallback);
   }, errorCallback);
}

function errorCallback(error) {
   alert("ERROR: " + error.code)
}

Export as manual build Cordova. Open the shell and go to the directory you exported to. Make sure you have Cordova installed. If you haven’t, you need to download NodeJS (search on Google) and type into the shell:

npm install -g Cordova

Then type:

cordova plugin add cordova-plugin-file

Now you can build it with phonegap build or the tutorial manually building with Cordova on the wiki.

  1. For the browser through JS:
    Use the HTML5 FileAPI.
    Example:
    Reading
// Trigger Once this bloc
let e = document.createElement("input")
e.setAttribute("type","file")
e.setAttribute("style","display:none")
e.addEventListener("change", function(){
    var reader = new FileReader();

    reader.onload = function(loadedEvent) {
        if (gdjs.evtTools.filereading === undefined) gdjs.evtTools.filereading = {};
        gdjs.evtTools.filereading.data = loadedEvent.target.result;
        gdjs.evtTools.filereading.updated = true
    }
    reader.readAsText(this.files[0]);
if (gdjs.evtTools.filereading === undefined) gdjs.evtTools.filereading {};
gdjs.evtTools.filereading.fs = e;

// Run each Frame this block

if (gdjs.evtTools.filereading === undefined) gdjs.evtTools.filereading {};
if(gdjs.evtTools.filereading.updated === undefined) gdjs.evtTools.filereading.updated = false;
if (gdjs.evtTools.filereading.updated) {
    runtimeScene.getVariables()
    .get("variable name")
    .setString(gdjs.evtTools.filereading.data)
}

// To actually load the file to the predefined variable
gdjs.evtTools.filereading.fs.click()

This solution requires the user to select a file from the explorer and is not the most user friendly option because of that.

thank you very much. I would stay in the desktop, without any java bull****. I cant find extension named “Filesystem” in the list of extensions. where can I get it? and why it is hidden?

You misunderstood me. There are 2 types of extensions in GDevelop and you are searching the wrong type. The filesystem extension is included with GDevelop and the actions/conditions etc are already in your list of actions/conditions etc

I fear that, you will say this :disappointed:. I just hope may be you know a filesystem extension somewhere hidden, wich is work. because I start the post about this built-in filesystem. I can’t use it, because it isn’t writen down how to. so, if you are still interested, listen to my story.

I’m almost done with my practicing project. I have some variable, wich I want to save. some kind of settings (for example volume). wich I want to remain after the user close the application. and of course load automatically, when the app is starting next time. I tought I will need a file on the dard drive, wich is remain to hold this information. but I didn’t work files before this in GDevelop. so, as normally I want to start with some learning. I go to the GDevelop 5 wiki / file system. soon it became clear, there is two different solution (extension) for this task. the file system to desktop and the (web)storage to web. of course it isn’t so simple, but I figured it out quickly, the file system is what I need (because my target platform is the desktop). I read the whole page multiple times. the first shock was that there isn’t a “create file” action! how will I save anything to the file, if I can’t create the file itself?! ok, the wiki is useless like usually, lets go to the forum, but this is just the pain in the *** again. spend hours to find something, wich may be isn’t there! because it shouldn’t be there, but in the wiki! ok, finally I found a clue here. someone told when you write information in a non-existent file, GDevelop will create the file automatically. ok, it makes sense, GDevelop will do this with variables, may be the same is true for files. lets try! I created this action to a new sub-event to my “at the beginning of the scene” event.


you will be surprised now, it isn’t create the file. as you can see I define a variable to the result. the funniest part of the whole, it give me an “OK” feedback. but I can’t find the file. as you can see it should be on my desktop. ok, I use win7, although I use it alone I have more than one desktop folder, maybe just misplaced somewhere. myself personally check all, but can’t find it. ok, may be there is more, hidden somewhere deep in windows. I ask the windows search, find it for me across the whole drive C, but nothing…

if you are still here, than say something for this now!

The problem is simple: you need the delimitor. What you did here would be on a windows machine something like “C:\Users\pc\DesktopCounterConfig.txt”, where you wanted “C:\Users\pc\Desktop\CounterConfig.txt”. To do that add the expression System path delimiter between the other expression and the filename.

And for config and such, localstorage is enough. Files are thought for when you need the player to have something to import (mods, localizations etc) that should be dynamically changed by the user.

ok, you’re right. I really missed the delimiter. but if I miss a required part, how can the file be created?! I got feedback on the success! and if it created succesful, why I can’t found? or if it isn’t created, how can I got a positive feedback?!

I don’t insist to an external file, if possible keep my values inside. but I can’t imagine this. could you explain how it works? how can be possible keep data without saving? after I close the program and turn off the computer?

1 Like

For the file, it was created, just not how you thought. If you look at what I said up, the file was created as DesktopCounterConfig, not CounterConfig. That is why you didn’t found it.

For the storage, it uses local storage wich is a storage stored with the browser’s settings and history for web application. It is written but into a local database. You don’t need to worry about anything like files, folders, it’s a key-value database. It means you can write for example the value “oof” to the key “Rob”. Then later you can access the key “Rob”, Wich contains oof.
The usage seems self explaining. Use the actions in storage built in extension.

1 Like

thank you for you patience. unfortunately I sill can’t imagine this storage. where are this local database? even if the exe is running in some kind of browser, this database should stored somewhere in the hard drive, if it’s last after I turn off my computer. isn’t it? otherwise it should disappear, isn’t it? and what happens if I clear this browser’s history? this data isn’t will disappear?

If you package with electron, there is no way to delete that without messing around. On chrome, it’s in the chrome appdata folder. It is possible to clear it from chrome, but tidious and nobody would do it without a good reason and knowing whate he’s doing.
You can find the localstorage data here on electron on Windows: ```
C:\Users<USER-NAME>\AppData\Roaming<APP-NAME>\Local Storage\leveldb\

There should be .log files in here. This is the database containing the data.
1 Like

ok. thanks for everything.

1 Like

one last problem (I hope). finally I choose the external file, for learning and practicing. ewerything went well as long while I use the desktop path. but before I compile the product, I rewrote it to game directory (where the *.exe is). this was the original plan, but I haven’t *.exe before. so I rewrote, compile (to zip and for Windows) and run, but the file isn’t created now. any idea? what did I do wrong?

Idk, but you shouldn’t be using the application directory, prefer the use of the application data directory.

1, why?
2, I can’t find the expression for this. are you mean userdata folder?

  1. Because the data is safer and easier to load from there, and faster. It is made so the program can access and store data quickly without struggling in a place where the user don’t need to know it’s here.

  2. Yes

I changed the output directory as you said and preview it. it worked well, but it isn’t use the newly created program’s appdata folder but the GDevelop appdata folder. I tought because this is just a preview. ok, lets build again! but before that, there is a few things what I forgot last time. so I rewrote the name and the version number in the Game settings\properties menu, and add icons in the Game settings\Icons menu. now ewerything is ready to build, I think, and start to export. but I got an error. what’s the matter again?

What is the error? Where is it?

I have a build log, but I don’t understand a **** word from it. its written in some alien language!

Send it? Maybe on pastebin if possible.