Save/Load Properties Script-UPDATE

Credits:
Linkxgl - Script (Basic Script)
TheDave - add on (‘Truncate’ and File Directing)
Blendenzo - add on (‘readline()’)

Hey guys, I made 2 scripts… a Save script and a Load script. These both are for properties, not for position or rotation because I find no point for those. I made them myself, with a bit of help from Blendenzo’s Save/Load scripting tutorial. These are for the lazy people who don’t like to read tutorials, don’t understand it or just don’t feel like writing a script. ENJOY! If you want to add something just ask, explain to me what it does and then I’ll add it to the script, then I’ll credit you, hopefully, when ever someone uses this in a game, they’ll credit us :slight_smile: It’s just something for everybody to use because I know a lot of people would like to make a cool RPG/Adventure, and some don’t know how. So, if you do contribute just don’t get mad if people use it, it’s the entire point.

If you just want the scripts here:
Saving Script:


#This is using GameLogic, so lets add a shortcut.
cont = GameLogic.getCurrentController()
 
#Get the script user, in this case it's the object you put this script to.
own = cont.getOwner()
 
#Make/save stuff in this file, in this case I called it "fileTest.test".
#PS: you can make it anything you want, but make sure to always have a extension, any will do.
#In addtion, you can write the file in a certain folder, thanks to TheDave
saveGame = open("C:\Program Files\Blender Foundation\fileTest.test", "w")
 
#This deletes everything in the file before it adds the new information.
saveGame.truncate(0)
#write the variable in the property, prop on the first line
#this is an int property
saveGame.write (str(own.prop) +"
")
 
#same thing as the above, but with the second property and line
#this is a string property... Yes, it makes a difference in the load script
saveGame.write (str(own.prop1) +"
")
 
#closing the file.
saveGame.close()

Load Script:


#refer to the save script for these.
cont = GameLogic.getCurrentController()
own = cont.getOwner()
 
#this line gives a shortcut for opening "fileTest.test" and read it.
#In addtion, you can write the file in a certain folder, thanks to TheDave
openFile = open("C:\Program Files\Blender Foundation\fileTest.test", "r")
 
#This means read the first line of the file and call it firstline
firstline = openFile.readline()
 
#Read the second line of the file and call it secondline
secondline = openFile.readline()
 
#close the file
openFile.close()
 
#give prop the variable found in firstline
own.prop = int(firstline)
 
#give prop1 the variable found in secondline, open it as a string.
#if I made it a int, the string wouldn't load.
own.prop1 = (str(secondline))

Here’s a more universal script. You won’t need to make different scripts for different objects. It’s more efficient, script wise, but if you saving a bunch of things, it gets really long, so I’ll try to figure out another more efficient way, Thanks and here they are:


<i><b>#Save Script</b></i>

#Giving GameLogic a shortcut
g = GameLogic

saveGame = open("fileTest.test", "w")

#Getting Object List in the current scene
objList = g.getCurrentScene().getObjectList()

#Giving name to objects in objList.
#Make sure to have OB before the object name.
cube2 = objList["OBCube.002"]
cube3 = objList["OBCube.003"]

#Writing them in the file
saveGame.write (str(cube2.prop) + "
")
saveGame.write (str(cube3.prop) + "
")


saveGame.close()

<i><b>#Load Script</b></i>

g = GameLogic

openFile = open("fileTest.test", "r")

objList = g.getCurrentScene().getObjectList()

cube2 = objList["Cube.002"]
cube3 = objList["Cube.003"]

firstline = openFile.readline()
secondline = openFile.readline()

openFile.close()

cube2.prop = int(firstline)

#Use (str) for a string property, otherwise use int
cube3.prop = (str(secondline))

Thanks man! :D:D:D

Nice work =)

From my experience with saving data, its best to truncate the file first so you don’t get remnants from a really long variable hanging around.

 
#write the variable in the property, prop on the first line
#this is an int property
saveGame.truncate(0)
saveGame.write (str(own.prop) +"
")

You can actually use any type of variable, including dictionaries this way. Simply swap


own.prop = int(firstline)

for…


own.prop = eval(firstline)

Hey, sorry, but can you explain this for me? I mean, just the truncate part, I didn’t get that. Thanks :slight_smile:

Sure. Truncate means to cut something short. For example, if we were to truncate the sentence “I love waffles” to 5 characters long, it would be “I lov”.

The trouble with .write() in your situation is it only replaces the characters equal to the length of your new variable.
i.e. if you had the number 567 stored, and wrote over it with the number 12, you’d actually end up with 127. If you truncate the file to 0 bytes length, you’re effectively making the file empty.

So, SaveGame.Truncate(0) fixes the 567 to 127?

No.

SaveGame.Truncate(0) deletes everything inside the file.

aahhhhhhh, thank you very much then, that’s very useful! Thanks alot for you contributation!

You’re welcome =) Glad I could help.

Thank you for that info about truncate(), TheDave. I found it very useful. I’m off to implement it in my current project now.

BTW, nice looking scripts you’ve got there, Linkxgl. I’m sure that they will be helpful to many.

Thanks, I wrote the save script my self, but the load script I used your tutorial, some person on BA, and my own knowledge. So, also thanks with your tutorial, without it I wouldn’t of known about readline(), because the load script I made, read the entire file, and It it’s a waste of space if I had to make different scripts for different properties.

I’ve used a script like this before, but I didn’t know about the “truncate” command… looks useful!

Yeah, it took me a little while to track down too. I used to delete the files and recreate them but very occasionally it would throw errors because the file was still being deleted as the data was getting written. In the 6-8months I’ve been using it, truncate hasn’t let me down yet.

If you’re interested, I could post the classes I wrote for saving and loading GameLogic variables. It’s pretty similar to what you’ve written, only its built to handle any type of variable.

Hey guys, I got 2 questions for you and hopefully some of you can answer it. Anyways, I need a code that saves your file in a certain destanation. For exmaple: write. C:\Program Files\File\save.txt
and to load: read.C:\Program Files\File\save.txt somting like that. Also, I want to know how to save things on certain lines. For example: write.property1 on line 10 of file save.txt. hopefully this isn’t hard to do, but I need it for a 3D Template that a BA member, named Cray, and some other people are helping to make. Thanks, I hope I can get an answer soon!

I can answer your first question. The name of the file is actually a path. Therefore I could write…

saveGame = open("fileTest.test", "w")

like…

saveGame = open("C:\stuff\fileTest.test","w")

It works using network addresses too.

As for your second question, I don’t know. I’ve always used a different file for each variable, thus only needing one line.

If you don’t set a fixed path then Blender will use the Current Working Directory. In your case that’ll be the place you’re running Blender from, or the place you saved your blend (depending on how you open your blends). This can be set with shortcuts using the “Start in:” field.

hey Dave and thanks, do you now if theres a way to make a new folder while at it, for example:
C:\Program Files\NewFolder\GameFile.test
So that blender makes the NewFolder. Or is that done with a setup wizard?

Try googling Python file input and output. Its quite a vast subject, and fairly easy to pick up. I imagine the first article you come across will answer all of your questions, and probably save you weeks worth of posting questions =)

I found the answer to your question by googling “python create directory” and clicking on the second
link.

One thing I’d suggest if you’re passionate about this (which you do appear to be) is installing Python on your computer and using the console it comes with to try out various things. It’s like a DOS prompt, but for python. You won’t be able to use GameLogic in it but you can play around with file manipulation and stuff.

One thing I’d suggest if you’re passionate about this (which you do appear to be) is installing Python on your computer and using the console it comes with to try out various things. It’s like a DOS prompt, but for python. You won’t be able to use GameLogic in it but you can play around with file manipulation and stuff.

Thanks, I’ve used python before, I can make very simple games in it (like pacman), but the save/load things is hard for me to get, it confuses me :spin: Anyways, I’ll be sure to do that :slight_smile:

Hey guys, I was just wondering if one of you could fix this script for me and explain what you did because I have no idea what to do. Hopefully I don’t have to fix much… but here it is, hope it’s not too messed up.

cont = GameLogic.getCurrentController()
own = cont.getOwner()

saveGame = open("File1.fil1", "w")

saveGame.write (str(own.money_count) +"
")

coinList = GameLogic.getCurrentScene().getObjectList()
["OBCoin"]["OBCoin.001"]["OBCoin.002"]["OBCoin.003"]["OBCoin.004"]["OBCoin.005"]
["OBCoin.011"]["OBCoin.012"]["OBCoin.013"]["OBCoin.014"]["OBCoin.010"]
["OBCoin.015"]["OBCoin.016"]["OBCoin.017"]["OBCoin.018"]["OBCoin.019"]
["OBCoin.020"]["OBCoin.021"]["OBCoin.022"]["OBCoin.023"]["OBCoin.024"]
["OBCoin.025"]["OBCoin.026"]["OBCoin.027"]["OBCoin.028"]["OBCoin.029"]
["OBCoin.030"]["OBCoin.031"]["OBCoin.032"]["OBCoin.033"]["OBCoin.034"]
["OBCoin.035"]["OBCoin.036"]["OBCoin.037"]["OBCoin.038"]["OBCoin.039"]
["OBCoin.040"]["OBCoin.041"]["OBCoin.041"]["OBCoin.042"]["OBCoin.043"]
["OBCoin.044"]["OBCoin.045"]["OBCoin.046"]["OBCoin.047"]["OBCoin.048"]
["OBCoin.049"]["OBCoin.050"]["OBCoin.051"]["OBCoin.052"]["OBCoin.053"]
["OBCoin.054"]["OBCoin.055"]["OBCoin.056"]["OBCoin.057"]["OBCoin.058"]
["OBCoin.059"]["OBCoin.060"]["OBCoin.061"]["OBCoin.062"]["OBCoin.063"]
["OBCoin.064"]["OBCoin.065"]["OBCoin.066"]["OBCoin.067"]["OBCoin.068"]
["OBCoin.069"]["OBCoin.070"]["OBCoin.071"]["OBCoin.072"]["OBCoin.073"]
["OBCoin.074"]["OBCoin.075"]["OBCoin.076"]["OBCoin.077"]

saveGame.write (int(coinList.appear))

saveGame.close()

saveGame.write (int(coinList.appear))

You’re saving a list as an integer. Also, whats .appear supposed to do?