2
\$\begingroup\$

Making a game with cocos2d-js, is there a reliable way to create saved data in my local computer? I see that there is cc.sys.localStorage but it is removed if the user clears the browser's cache or when they simply use another browser.

\$\endgroup\$
1
\$\begingroup\$

With JavaScript you don't have a way to persist things transparently other than local storage. I don't know anything about cocos2d but this is really a question about JS. If you really need something persisted you have a couple options.

First you can fake your browser into thinking you want to download something, you can do this by using a library like https://github.com/eligrey/FileSaver.js. You won't have any control over where the saved file lives on the machine though and likely won't be able to read it back in unless you have the user navigate to it themselves.

The second option is just to accept localStorage's limitations. Depending on what you are trying to achieve this is probably a good way to go, just know that not all browsers support it and the amount of data you can store is limited.

The third option is to use a server to handle saving the file. I know you asked for how to store something locally, but I think this is your best bet unless you can't require an internet connection. You can write your own or use something like Firebase which is awesome and has a free tier that is pretty solid.

\$\endgroup\$
1
\$\begingroup\$

Most web browsers implements kind of JS Sandbox that blocks any interaction with the real machine, JS just has power over the webkit/gecko/... layer that works like a "virtual machine", that's all, any interaction with the host is blocked by default.

Anyways there are ways to interact with the OS layer and the filesystem via JS via some tweaking like creating an browser plugin, tweaking and compiling your own browser to build a bridge between the browser engine and the host or using webkit standalone implementations like nodejs/nwjs.

Most mobile games that are built in JS uses a hibrid implementation that runs the JS code in a embedded browser in a native aplication, some of them has it's own API to access the ANDROID/IOS API allowing the interaction with the device from JS.

So no, you can't get control of the filesystem without a third app/plugin/... from a webpage.

To store raw data in a device there are some options, the well-knonw coockies, the localStorage and the sessionStorage, but you can't write permanent files.

IMHO, To get rid of your problem i would give a option to the user to download the .sav file (a .json with all the data you want to store) and give him the option to load from a .sav file.

(.sav is just an example, the extension doesn't matters)

hope this helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.