saving game data

BlitzMax Forums/BlitzMax Beginners Area/saving game data

delusan(Posted 2007) [#1]
Can someone explain the basics of saving game data so that it is available the next time the game is played?


H&K(Posted 2007) [#2]
open/create a file. Write the data to it. close the file

Open a file. read the data from it. close the file.

Someone is bound to give you a link, or some code. But I would recomend just sitting down and trying to do the above. Even if you then use someonelese code you will understand it better because of the work you put in doing your own


ImaginaryHuman(Posted 2007) [#3]
Doesn't it get all convoluted with regard to where you are allowed or supposed to store the data, mainly on windows?


JazzieB(Posted 2007) [#4]
No. Even on Vista a program is allowed to write to its own folder.

You can of course play it safe and obtain the user's folder or applications data folder and store the information there. I also make the additional step of using SetFileMode to give read/write rights to all user types after saving the file, as this solved some issues I was having on the Mac and Linux.

As for the original question, you need to work out which variables, arrays and so on hold the entire game state at any one time and save those to a file. You then load them all back into the original vairables to restore the game as it was and start your main loop again.

What you need to save will vary greatly depending on the style of game and whether you can save anywhere or just between levels. Basically, you will need to save all the information regarding the player (score, lives, level, etc), any enemies (position, energy, etc) and the level itself (or the state of anything that can change). If you only allow saving between levels, then only the player info and the starting level number is needed. If you allow saving anywhere, things get complicated in that you will have much more to save.


kronholm(Posted 2007) [#5]
Can your program even write to c:\document settings\profilename\application data\ on winxp/vista?

Obviously on linux it can write to /usr/username, but is there a generic settings folder like on windows?

Mac?


bradford6(Posted 2007) [#6]

can someone explain the basics of saving game data so that it is available the next time the game is played?"



essentially, it depends on what you are trying to save and what you want the player to experience when he/she resumes play.

besides the file detail mentioned earlier, you could look into the following:

1. you will probably want to save gamestate. meaning the locations (x,y,z) of all the game assets at the time of the save. Sometimes you might not want to do this. i.e if there are alot of moving enemies in your game, you might not want the player to be able to incrementally save every 3 seconds after killing an enemy.

2. you'll want to save the game logic. (I.e if a button was pressed that opened a door you'll want the level to load with the door open and button pressed)

with that said, you might want to have an autosave feature that saves the players progress at certain points (especially after a challengin part) or at least stops and asks (do you want to save now?)

depending on your level of expertise, you might want to look at Brucey's LibXML module. you can save game to an XML file.

If your game assets are organized into Types, the Save/Load functions should be easier. as you could just save the Field Data for the game assets.


FlameDuck(Posted 2007) [#7]
Can your program even write to c:\document settings\profilename\application data\ on winxp/vista?
Yes.

Obviously on linux it can write to /usr/username
Actually no. Not usually (since there isn't such a directory, and the current user won't have write access - unless they have root privileges). It can write to /home/username tho'.

but is there a generic settings folder like on windows?
Yup. It's called /etc. And there isn't a generic one on Windows. Depending on what you need to do, there are at least 3 Single Points Of Truth as to how you could save such data persistently with Windows.

Mac?
Dunno. Reckon it's the same as on Linux.

Basically I think that the XML serialization bradford6 is hinting at is probably the easiest solution - although somewhat prone to abuse - if that's an issue.

Also I suppose you could use SQLLite or something similar if you wanted to use a relational dataset.


WendellM(Posted 2007) [#8]
Can your program even write to c:\document settings\profilename\application data\ on winxp/vista?

A Microsoft page on Vista's restrictions ("User Account Control for Game Developers"):

http://msdn2.microsoft.com/en-us/library/bb206295.aspx


delusan(Posted 2007) [#9]
folks - thats a lot of information and much appreciated - however, your levels of expertise are MUCH higher than mine ... i just want to learn to write a save/read back feature for my modest games ... a what BMax functions are used and how are they used type thing. I followed up your information with a search and that information + what you have shared with me here enabled me to write my own (rudimentary) code that works in my program. Much thanks to you all !!


tonyg(Posted 2007) [#10]
Have a look in the streams module. You'll need to openstream, writestream, readstream to create or ready the file for reading/writing and then the appropriate read/write command for the type of data you want to read/write.


kronholm(Posted 2007) [#11]
This may be what you're looking for..

Say you have a game with bricks. You need to load a level that's saved. You create a function for creating the bricks, and you create a function to load from the file. Then you write a parser function which interprets what's in the file and creates the brick from the data. For instance the data in the file could be something like
1,100,200,255,255,255:2,400,100,200,200,200. The : forms as a seperator for you when you read it so it's easier to distinguish when a new brick starts and ends. The format is id,x,y,r,g,b in this case. It's pretty simple once you play a bit with it, and don't forget to use openfile and closefile.


Grey Alien(Posted 2007) [#12]
WendellM: Thanks, just what I needed.


WendellM(Posted 2007) [#13]
Glad to help, Grey. I was pleased to find that today after all the uncertainty as to exactly what Vista does and doesn't allow. It'll likely be a while before I get Vista (I'm putting it off as long as practical), but that looks like it fills in the blanks.


Grey Alien(Posted 2007) [#14]
yeah it's nice and specific.


JazzieB(Posted 2007) [#15]
Hmm, that document makes interesting reading. However, some of what it says seems to contradict my own tests...

I have a simple Space Invaders game that saves a high score table in the same folder as the game. According to that document, when the game is run from a standard account with UAC turned on, and assuming that BlitzMax apps are running in legacy mode, the high score table should be saved in the virtualised Program Files folder (if there isn't a file already present in the normal folder). I've just tested this and it creates a new scores file in the same folder as the game!

So this poses a bit of a question... Are BlitzMax apps given admin rights and/or are they running in legacy mode under Vista? Or maybe it's down to the installer I use, which may give the necessary rights to programs saving files in their own folder. These questions probably should be asked/answered in another thread though, as it's starting to get a little off-topic.

The question being asked at the top of this is HOW to save games, not WHERE. Probably confused the poor guy now!


Grey Alien(Posted 2007) [#16]
yah we've hijacked a bit but he seemed to be happy with the answers already.

Must admit that your saving thing is pretty weird. Why not just try your app in it's own folder without using an installer?


JazzieB(Posted 2007) [#17]
Yeah, I have thought of trying to install manually without an installer, but in no rush at the moment. Simple reason that all my final releases are for the general public, which will all be in installer form anyway. Having said that, I will try at some point.