123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|603|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> Emscripten - Saving Data

Sun, 23 Dec 2018, 08:47
Jayenkai

Emscripten - Saving Data


*Saving an array of data, integers, binary, bytes, etc, into a single string, and storing it in localStorage, for the purpose of saving highscores, scores, progress, scoreboards, etc.*

This has NOT been an easy task!
I'm sure there's "better" methods for this, but I sure as hell couldn't find any. It doesn't help that Google is getting worse at doing its job, leading to me having strings of "Emscripten" "webgl" "save" and 100,000 other combinations, in a desperate search for the solution.

The issues are numerous, but the most difficult issue is that I can only find decent methods to save "big string" to localStorage. localStorage can easily handle 0's in the string, but the Emscripten EM_JS command (which you use to embed Javascript into c++ code) does NOT like 0's in the string. Instead, you're limited to converting ints to strings, and clumping them together, writing that as a great big string.

I figured, if I'm going to save "A great big string", then I might as well go the whole hog, and turn my numbers into slightly better organised strings.


I created a large char FakeFile, and from there created a couple of functions to read and write bytes, converting each byte (0-255) into a two character Alpha-Numeric string.



(Please note, I haven't neatened this code up, in any way!)

Read and Write of ints works using the Byte function, working on the 4 bytes of an int.
Strings are currently not working, because I CBA figuring that out!!

ReadFile() and WriteFile() simply reset the big variable, along with the two location variables, and with ReadFile then grabbing the stored string from the user's localStorage.
CloseFile() will store a WriteFile string onto localStorage when called.




These two functions then do the magic, making use of Javascript's localStorage to save/retreive the data.
The EM_JS creates functions in Javascript, which you can then call directly within C++. eg, WriteFakeFile("Lemonade","Waffle Waffle Waffle",20);

And.. I think that's all I did.
It took me two solid days of coding to do it, and it really feels like I've done naff all!!!

Perhaps posting this may help someone in the future..
Or maybe it's a big pile of garbage, and there's actually a much more obvious way to do this!!

If you know of any, do let me know.

-=-=-
''Load, Next List!''
Sun, 23 Dec 2018, 09:21
Pakz
Have you tried searching directly in sites like github? I also have my doubts on the google engine being so good.

I hope someone adds or shows how to save data with monkey2 emscripten for when
I get back to working on that voxel minecraft thing.
Sun, 23 Dec 2018, 09:27
Jayenkai
Has Mark not added saves to Monkey2? .. My horrorshow with it probably explains why!!!

-=-=-
''Load, Next List!''
Mon, 24 Dec 2018, 05:10
Pakz
The mx2 site has been down for days now. A spammer started flooding the forum before that. I had asked on the forum for a example for emscripten saving a while ago. Sometimes questions never get answered. Like how I could play amiga and pc module files with the build in sound module.

I do know that emscripten wil become the focus point of monkey2(fast compiling coming) So saving would bound to be in there.
Mon, 24 Dec 2018, 05:21
Jayenkai
I tweeted a link with Mark tagged, so .. I dunno if it'll help him at all, but if he's aiming to get it working, it'll probably be a half-decent starting point.

Today I attempted to switch on my standard method of Online Highscore Submission...
... Looks like that'll be another big task to figure out!

You'd think "Game that's already online" would be able to cope with "Send data to script on the same server", but. Nope.. Not with Emscripten!!

To be fair, it should be a reasonably easy task with my newfound Javascript->c++->Javascript embedding powers. I just need to rethink how I'm going to access the data going in either direction.

-=-=-
''Load, Next List!''