Save and load game in a specific folder using NW.js (Node-Webkit)

8

Stats

9,145 visits, 13,877 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 7 Nov, 2015. Last updated 19 Feb, 2019

Hello everyone !

This is my first tutorial and English isn't my mother tongue, so please don't hesitate to point out to me any mistake I make !

Node-Webkit is a standalone version of the Google Chrome browser.

It allows you to create autonomous desktop applications (it's doesn't run in the user's browser but in a seperated window instead. Furthermore, desktop apps have unique privileges not normally available from web apps, such as the ability to write files directly to disk.)

For more information, I recommend you to have a look to this:

https://www.scirra.com/tutorials/1276/exporting-desktop-apps-with-node-webkit

/!\ This method will not work if you try to preview your project on a classic browser such as Firefox or Google Chrome, that's the whole point... So make sure your preview browser is set to "NW.js" in your project properties. If that option isn't available for you, check the link above to learn how to install it. /!\</p>

In this tutorial, I will explain how to save and load your game in a specific folder using NodeWebkit :

Getting started

First, you will need to insert the NWjs object to your project.

Then, you will need to create the folder where you want to save the game. In this example I will name it "Test".

As explained in Construct 2 manual, it is strongly advised to save files to the user's folder, which you almost certainly have write permission for. That's what we will do here.

I used "on loader layout complete" trigger here, so that the game creates the "Test" folder if it doesn't exist yet in the user folder when the game is launched.

The SaveGame() function

As explained in the "How to make savegames" tutorial

"In both the "On save complete and On load complete triggers", the system SaveStateJSON expression returns a string of all the JSON data for the savegame. Note the SaveStateJSON expression will return an empty string outside of these triggers; the triggers are your only opportunity to access the data directly."

That is why you will need to use the "save" action to get a string of all the JSON data for the savegame, and then write it in a file.

I named the file where I saved the JSON data "Slot1.save", but if you want multiple slots you will need to create a Global Variable to store the number of the current save slot.

Also, I named "test" the slot for the save action, but that's not important.

The LoadGame() function

To load the game, you simply use the "System | load game from JSON string" action and you use NWjs.readFile to read the data in the file you created with the SaveGame() function :

Test your functions

You can test your functions with keyboard-triggered events :

Conclusion

That's it ! Now you can save and load your game in a specific folder using Node-Webkit.

The main defect of this method is that you need to use the system Save action, which means that there is another savegame somewhere on the computer. I don't know how to delete it, but if someone can clarify that matter for me I will gladly update this tutorial to solve the problem !

Don't forget to add the No Save behavior wherever you can to make your savegames smaller and faster to save and load !

  • 3 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Thank you! Merci!

    I had a hard time to understand how to use the nw.js expressions, it is really clear now :)

    About the save file created by save functions, I made my researchs and located the folder where the save is written, you have to access the local storage of the app with %appdata% to do so, then I suppose you just have to write on the text file to make it empty ^^

  • Hi! Thanks for the tutorial. I followed the directions and it creates the folder and file, but doesn't load it. Did I make a mistake?

    i.imgur.com/pAZe7tI.png

    (I have a set of objects that you click each with a different index setting the slot number)