zazazayou Posted November 15, 2017 Share Posted November 15, 2017 I have been working on a game in javascript, and I'm stuck as to how to handle saving and loading. I want to ultimately publish it both as an online browser game and as a downloadable desktop application (which I'd do with electron). But for now it is just a browser game. You can see it here by the way on my github page: https://jpchaufan.github.io/ What I have going now is not really working. I tried using local storage. The problem is that the game is kind of minecraft-like, in that it has an infinite world, that keeps generating as you walk around. When I stringify the world data into JSON, it is huge, like easily 6 million characters long, and potentially way more as the player walks around even a little bit. As a result, saving the game takes FOREVER, and practically crashes the browser if the world has been explored much. I also tried another alternative, to try and avoid the limitations of local storage. I tried just printing out the save data for the player to copy and save manually themselves. Then give them the option of loading the game from local storage, or letting them copy in their own data to load. This didnt really solve the problem. It still takes forever to print the data out, and lets the player cheat too easily. I tried encrypting the data but it already takes too long to do, so it just made it worse. Now I am hoping that this becomes easier on a desktop version of the game, or if I actually put the game online on a real webpage with a back-end. I actually would hope that the game can auto-save, so that the player can't go back if he dies or messes up. I mean my game is pretty simplistic compared to a game like minecraft or any MMO that has a big world. Quote Link to comment Share on other sites More sharing options...
mattstyles Posted November 15, 2017 Share Posted November 15, 2017 Minecraft (I don't think) saves all those blocks either, nor does it have to, if you've used git source control then you'll be familiar with the concept of 'diffs', which I think is how Minecraft saves world data. In any case the following limits the amount you have to save, you need 2 things: * A pure deterministic world generation algorithm * Understanding how to store diffs against the deterministic world The process is actually really simple: Generate a seed (number, string, anything, doesn't matter) and use that seed to generate a world, the algorithm must be pure (referentially transparent) i.e. the same seed always produces the same structure (world map), an easy way to achieve this is to use the seed to seed a random number generator. No-one wants to make their own RNG, so pick one out there that allows supplying a seed (note that RNG is proper tricky, inside a computer people actually use its normally fairly easy to maintain entropy, on a server it can be trickier, mostly whatever OS you are running handles keeping entropy up). If your world map is immutable then you only ever need to store the seed, that'll deffo fit in local storage as it'll likely only be a few bytes. In this case it doesn't matter how much of the world has been explored on a previous visit, you can generate it on the next playthrough from the stored seed, so long as your algorithm is pure then you know the world will always be the same, so long as you can generate the world fast enough as your player moves around you're laughing but you'll have this problem anyway. If your world map is mutable (like vanilla Minecraft) then you'll have to work a little harder. Storing everything you've generated is the naive approach and, as you've noticed, doesn't work very well. The thing is, not much of your world will actually change, so just store the changes, this vastly reduces the problem space. Just store the diffs between world gen and current state. You'd also want a way to overwrite previous diffs where necessary, i.e. I place a stone block at [10, 12], then a wood block in the same place, well, you only store the diff between world gen and placing the wood block and nuke the previous diff that mentioned the stone block. On the next (loaded) play through you generate the world using the seed, then apply the stored diffs to regain where the player left off. To further reduce save/load times save/load only what you currently need i.e. when a player leaves an area save it, when they enter an area load it. jay3sh, zazazayou and b10b 3 Quote Link to comment Share on other sites More sharing options...
zazazayou Posted November 17, 2017 Author Share Posted November 17, 2017 thanks, that sounds like its exactly what i need to look into Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.