Save/Load Properties Script-UPDATE

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?

appear is a common property in all of the objects listed above. I realized that it doesn’t work this way, but there should be a way of getting all of the same properties and save them in a file. [test, test2] and I’m guessing this is how it goes together. Do I use the getProperty function or the getObjectList function? I don’t know.:spin:

So this is what I need for example:
Test1 and Test2 have the same property called “game”
I want to save both of the properties, but as two seperate properties. So in the file the property in Test1, game, was a string “start” and Test2’s property, game, was a string called “run”. In the file I would want to save like this:

start
run

in order. And I was wondering if my way was correct, but I figered out it wasn’t… And I’m wondering if anybody knows how to do that. Saving a group of properties individually.

Hey guys, I just figered it out, but I’m still wondering how to read certain lines.