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

Stats

3,270 visits, 5,992 views

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. /!\

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 !