Advanced Save/Load?

Hello,

I’m making a game and I’m wondering if you can save your game progress,
and when you go back to the loadmenu, you can see your game progress
(like the name of the level).

The only problem is that I only have experience with saving properties :frowning:

I hope some one can help me!

-Pret_Letter

You can save anything that way, everything is “properties”, that is, if there are enemies or not, position, velocity, orientation, time of day, etc.

allright , plantperson made zarks which have good save functionality, and released the blends …

and also , there are bunch of pre-made save-load blends , search for them and learn from the blends …

hope that helps .

You’re going to have to use Python.

One of the best ways to write a file, is:


mahfile = open("save.cool", "w")
mahfile.write("this is mah data! it goes here! 
 It has to be a string!")
mahfile.close()

You can use the pickle (or preferably cPickle) module to convert data into strings. There are tutorials on it all over.

Also, notice how Chaser used an extension other than .txt for his file. This dissuades a gamer from editing the save file.

If you want to go all crazy about encryption, you could md5 the thing:

import md5
m = md5.new()
m.update("Hello World!")
m.update("Goodbye World!")
encodedstring = m.digest()

"this is mah Data’s! It’s a firin’ mah laser!!! BLAAAHHHH!!!

…sorry, carry on…

I didn’t know what Pickle did.
But, I think loading something from pickle is kind of complicated. It uses multiple lines.
I think it’s just better to stick using str()…
How do you LOAD MD5 encoding?

Pickle is very easy. I suggest using cPickle, because it’s identical, but faster.


import cPickle

# turning the data into a string
mahdata = [3, 4.0, ["laza", "beams"]]
uberString = cPickle.dumps(mahdata)

# turning the string back into data
mahNewData = cPickle.loads(uberString)

Is cPickle something which can be downloaded for use in blender? I haven’t heard of it before.

Also, using an extension other than .txt - I didn’t know you could do that. Very interesting…

And perhaps this could be another option - http://lists.blender.org/pipermail/bf-blender-cvs/2008-August/015394.html

It doesn’t work with the blenderplayer yet, but it looks promising.

Pickle saves things as multiple lines, so it shouldn’t be really easy to separate objects unless you do something like split("

“) or split(”;")…

cPickle (and pickle for that matter) come with every installation of Python. As long as Python is installed, these modules should work fine.

Awesome. But what about when using a .exe runtime? Does the player have to have python installed on their computer?

why not save the file as an xml document? It’d be the easiest way to save multiple properties into a text file and then load them again…

example of the .xml file data:

<gamesave level=“laboratory” date=“28-august-2008” playhours=“6:22”>

<character>
<life>97</life>
<money>200</money>
</character>

<inventory>
<object name=“pen”/>
<object name=“tape”/>
<object name=“rock”/>
<weapon name=“rifle” ammo=“4”/>
<weapon name=“sniper” ammo=“22”/>
<weapon name=“handgun” ammo=“56”/>
</inventory>

</gamesave>

You can have multiple attributes per node, any node name you desire… and in comparison to using comma’s to delimit the text file, it’s an absolute piece of cake to save and load (especially is you use something like sax to help you with parsing it) and it’s easily extensible when you realise you have some extra piece of complicated data to save.

chaser , can you give us details about md5 ? please , thanks …

Whipster, what functions do you use for loading and saving XML documents?
Thank you

Hi B3D00 - To answer your question, it really comes down to preference. You could use SAX or DOM (or mini dom). I prefer DOM and miniDOM (which is even easier), because it allows you to treat the file as a tree - which is handy, and easier to de-bug in your head.

To Load a file, something like this should work.


from xml.dom import utils,core
import string

# Read an XML document into a DOM object
reader = utils.FileReader('Somefile.xml')

# Retrieve top level DOM document object
doc = reader.document

# Loop through the nodes ()
for n in doc.documentElement.childNodes:
 if n.localName == "somenodename":
  # lets say you have a node with a collection of nodes
  # nested inside, you could test for a certain localName
  # (node name) and then do for i in n.childNodes - and then loop through
  # the children (thats where the tree structure comes in handy!).
  
  print n.localName

to write an xml file then you could just do something like this:


from xml.dom.minidom import getDOMImplementation
 
 dom = getDOMImplementation()
 doc = dom.createDocument(None, "root", None)
 
 # the top root node called "root" by the line above
 # could be any name you choose though.
 topElement = doc.documentElement

 firstElement = doc.createElement("anyname")

 #append the element created above as a child of the root node
 topElement.appendChild(firstElement)

 #append a text node as a child of the 'anyname' node
 textNode = doc.createTextNode("Some text here")
 firstElement.appendChild(textNode)
 
 xmlString = doc.toprettyxml(" " * 4)
 #then print out the xmlstring variable to a file

which would produce something like this:


&lt;root&gt;
       &lt;anyname&gt;
                some text here
      &lt;/anyname&gt;
&lt;/root&gt;

I hope this helps - It may look sorta daunting, but seriously it’s alot more versatile and easier in the long run than coming up with your own ‘filetype’ and crudely parsing it as best you can with commas to seperate values etc.

I haven’t tested the code, so don’t shoot me if it doesn’t work! but you should get the gist of it. If you need more help with it just PM with some details of what your trying to do and I’ll write up some example code for you.

Whipster.

Do you mean decrypt the encoding to the text that it was before the MD5 checksum was performed?

If that’s what you meant then I’m afraid to say that it’s not possible. MD5 isn’t meant to be used as an encryption method really (although there are instances where it may be useful), but it is useful for checking to see if a file had been altered or tampered with.

You could save your file, perform a MD5 checksum on it, and save the hash result somewhere else. Then, when the file is being opened, you first checksum it again - If it matches the hash from before then you know that the file has remained intact… but if the file has been tampered (even if a space has been added to the end) then the checksum from the file on load will be drastically different from the checksum hash from the file when it was saved - alerting you to the fact the file was altered or corrupted in some way.

a case where it may be useful for encrypting though is if you were saving passwords to a file. The user submits their password, and to store it you save the MD5 checksum - instead of the actual password (which would have security implications for obvious reasons).

Then when the user wants access to the ‘system’, you take the password that was typed in, hash it, and compare it with your password list.

If you want to do encrypt and decrypt try something like Base64