-
Notifications
You must be signed in to change notification settings - Fork 927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add load/save functionality to the FileSystem extension #884
Conversation
Looks like a great start 😊👍 |
I am contemplating about how to code the loadJSONFileFromFileSystem (as an equivalent to loadJSONFileFromStorage). |
Rename "text" to "string" in javascript functions Make "err" a separate argument in console output
I used the asynchronous variant of the unlink (delete) function to not block the game. |
Yep, that's a good idea :)
Let's avoid accessing gdjs.evtTools.storage, because it's a quite specialized extension and you can even consider that it's not guaranteed that this extension exists (even if it's builtin, at some point it could become an extension like any other). But I'm not sure why you're dealing with JSON here? As this API is low level, and as you've added If someone want to load JSON, the user can:
If the user want to save JSON from a variable:
By the way, what I wrote here should go in a "how to" page on the wiki :) There are already a few explanations about it here: http://wiki.compilgames.net/doku.php/gdevelop5/all-features/network This being said, maybe it's also good to provide, in addition to previous methods, two functions to save/load directly a variable from/to a file containing JSON (let's call them
(it's using gdjs.evtTools.network, which looks like private apis, a bit like gdjs.evtTools.storage. The only difference is that I plan to refactor these two functions to put them later into gdjs.Variable. I've added "TODOs" comments to do this - but never took the time to do it. Anyway, you can use them freely - there will always be there, they are builtin in the game engine :) If I move them at some point, I'll change them in the whole codebase). What do you think? Does that makes sense? |
Yes that makes sense. |
- Sync and Async
At first I went with the Async file loading but I think it is pretty confusing for non programmers when you load the text in one action and try to access it in the following action of the same event but get an empty string instead of the expected file content. |
Does |
Yes that's a good idea to have a default "sync" action, and have an async version for more advanced users and larger games/files.
Yes, it's recursive. You get a JSON representing the whole variable (and can "decode" back the JSON to a structure variable which will be exactly the same, using the other method
Yes, you dump the whole JSON converted from the variable directly into the file without any transformation. JSON is self sufficient by itself :) |
The code looks pretty good! :) Added a few comments, one about adding support for an error variable. I think it would be useful to make robust games that warn the user if something wrong happened. It's something that I did for the Facebook Instant Games actions, and also a convention that we can use for other asynchronous actions and actions that can fail. |
More pleasant formulation of descriptions
I have added the optional variable. But not just for errors but also for success. |
…structure Add function to save a structure as JSON string into a file Add sync and async variants of the existing functions Cleanup variable names and error result
You're not writing JSON ;) You're writing a text, that happened to look like JSON. Hence the quotation mark. This is JSON: But variable are text or number. If you enter a number, then a number is stored. Otherwise a text is stored. If you write {"hello": "world"}, GDevelop will store in memory the text And this is a good thing! Otherwise, imagine someone writing as its player name The equivalent of JSON in GDevelop are the structure variables.
There is now way to sort them. It should be done in EnumerateInstruction.js in the IDE. |
I think you got me wrong. What I mean is that when I save the structure with quotation marks inside as the content of the text variable into a file and try to convert it back into a structure, I would expect it to escape the quotation marks when saving to file and recreating the original structure with the quotation marks inside the string variable.
But instead the function |
Very strange then... Something is weird. Can you post here the content of the JSON that is outputted? :) |
@Wend1go My bad, I've verified the implementation of gdjs.evtTools.network.variableStructureToJSON, and it's not escaping at all the content of strings 😡😕 Totally misunderstood your first message reporting this issue. Can't believe how I did not do this on the first place... Anyway, let me work and push a fix on master for this. |
This is what gets parsed into the text file:
And this is the error of
EDIT: |
I think this should be ready for a review then. |
Yup, gdjs.evtTools.network.variableStructureToJSON is outputting totally invalid JSON. |
Merge from upstream
Cool, let me know if you can merge master in your branch, to see if the issue with the variable containing JSON being converted to JSON is fixed (and more generally, the issue of variable containing quotes, backslash, new lines being converted to JSON).
Yes that's fine :) |
Nice! Let me review the code now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic of the code sounds good.
A few changes that I've noted though, that all boil down to keeping things consistent:
- We should always specify JSON file or "file ... as JSON".
- We should always write "scene variable". The fact is that the variable can be a structure, a number or a string, but this does not matter. This is especially important for users as they might be confused about is this a global, scene or object variable?
- Name of the functions internally should also refer to variable, and not structure.
- A few typo: finished, asynchronous - make sure to replace them in the whole file.
Hope it make sense :) Thanks for you work on this so far.
The wiki article has been updated. I will try to resolve your findings this evening. I'm lacking the rights to add the article about the file system extension (and screenshot extension as well) to the "all other features" article. The articles can currently only be opened via the link inside the IDE. Not even the search field in the wiki brought the articles up. And as always: If you are a native English speaker and want something to laugh about please proof read ;) |
Thanks for writing it!! :) Will add it to the all features page. Search index is updated everyday by Algolia afaik. |
I guess that was all, thanks for reviewing 👍 |
Cool, will merge if there is no major issues. Just started re-reading the File System wiki page. The Storage vs File system is a good idea and will surely be useful for a lot of users :) |
I've also added a few notes with links on the storage pages about Filesystem :) |
One more thing before merging: |
* Fix an action wrongly mentioning JSON * Add "asynchronously" where needed in descriptions * Always use scene variable for consistency, only mention structure as an information. * Update name of functions to remove reference to structure, use variable instead as it's the type expected in the JS functions. * Run Prettier
* Typo asyncrounousely => asynchronously * `scenevar` is not a proper type in JavaScript, use `gdjs.Variable` instead * Remove "optional" in documentation are these variables as not optional arguments in JavaScript.
I've pushed a few fixes for typo and descriptions, ran Prettier on the files, changed mention to structure in the code by variable - while keeping a mention about structure in description of the actions so that it's clear that structure are properly converted to JSON and vice-versa. |
Merged :) |
Just released beta 63 with these new additions. If anyone on Windows can test would be great. I'm not worried at all though, Node.js apis are very portable and never had an issue with them on GDevelop itself. |
@Wend1go |
@Wend1go maybe another thing. |
This is due to lack of support for arrays in GDevelop structures. Arrays are converted to objects with keys "0", "1", "2"... |
The save / load functions can be used with Json strings just like the Storage extension does.