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 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))