How to save position and orientation of object

Please can someone tell me a script which save(when i shut down game) and load(when game start) position and orientation of object.i cant found it and i am desperate…simple example in . blend will be great…THX

May be this will not the exact solution,but it will work.
Write a script with 2 function, Funtion one will take up the Object Matrix and write to a file(txt file) function 2 will read the object matrix from the file(made by the function one) and set it for the object.
Funtion in Object module:
getMatrix() ;setMatrix()
Hope you have understood,it is similar to object serialization.(It has nothing to do with your
work)

Blendenzo wrote a very nice tutorial on the matter:

http://www.blendenzo.com/tutSaveLoad.html

He plans to put up example files, but really, you should have no problem just setting it up from the code that he has posted there.

Hope that helps.

Can somebody make example in to the . blend with simple cube…???i am not lazy to do this with blendenzo tutorial but i absolutly dont understand python and all programming… it is crucial for my project…i can make some models for my saviour…THX

Here: SaveLoad.blend

In this example, the position and orientation of the cube are saved to an external file called “Game1.sav”.

Use the arrows to move the cube.
Press “S” to save, press “L” to load.

Note: This example will not work with Blender 2.25 and older, because I used the GameLogic.getCurrentScene() function. It should work fine on newer versions. (I don’t know exactly which version that function was introduced in, though.)

thx much …but it save only position right??? how can i save rotation???

yes!!! it is exactly what i want…can you publish this script…or is it possible read it in . blend file??? can you lightly describe how to make this script with more than one object??? thx

Awsome example blendenzo. Very clear, well made.

Try Shooting Gallery v242 in blender games and demos.
It saves high score. You try make it save the game.

Yes, the scripts can be read in the .blend file. In the text editor window (the one on the right with the instructions on how to use the file), choose the file “Save.py”. It is the script to save the position and orientation.

http://img20.imageshack.us/img20/47/switchscriptszs6.jpg

“Load.py” is the script for loading the position and orientation. You will need to change the part at the the top of each script to fit your needs (the part where you list which objects to save data from). Object names need to be in the format “OBObjectName”. The quotation marks and the letters OB are very important, so make sure you include them.

Even with this example file, it is still going to take you a little bit of Python work to get it integrated into your game.

please blendenzo can you publish one more exemple of script with two object to save and load???it is my last problem…thanks much

i tryed change the script for second cube “cub” … but it save only “cube” numbers…please why???

Add objects to be saved to this list

cube = GameLogic.getCurrentScene().getObjectList()[“OBCube”]
cubePosi = cube.getPosition()
cubeRot = cube.getOrientation()

cub = GameLogic.getCurrentScene().getObjectList()[“OBCub”]
cubPosi = cub.getPosition()
cubRot = cub.getOrientation()

Open the file “Game1.sav” in write mode

saveFile = open(“Game1.sav”, “w”)

Write header to file

saveFile.write("This is a valid save file
")

Write game data to file

for x in range(len(cubePosi)):
saveFile.write(str(cubePosi[x]) + "
")
for x in range(len(cubeRot)):
for y in range(len(cubeRot[x])):
saveFile.write(str(cubeRot[x][y]) + "
")

for x in range(len(cubPosi)):
saveFile.write(str(cubPosi[x]) + "
")
for x in range(len(cubRot)):
for y in range(len(cubRot[x])):
saveFile.write(str(cubRot[x][y]) + "
")

Close save file

saveFile.close()

@everybody:
Hello,
here is a little easier way(the only real difference resides in the use of the eval() function which purpose is to evaluate a string as a python instruction_I looooove this function :eyebrowlift: ):
below the two modified scripts:

### Add objects to be saved to this list ###
cube = GameLogic.getCurrentScene().getObjectList()["OBCube"]

### Open the file "Game1.sav" in write mode
saveFile = open("Game1.sav", "w")
### Write header to file
saveFile.write("This is a valid save file
")

### Write game data to file
saveFile.write(str(cube.getPosition()) + "
") 
saveFile.write(str(cube.getOrientation()) + "
")

### Close save file
saveFile.close()
### Add objects to be saved to this list ###
cube = GameLogic.getCurrentScene().getObjectList()["OBCube"]

### Open the file "Game1.sav" in read mode
loadFile = open("Game1.sav", "r")
### Check for the header
header = loadFile.readline()
header = header[0:-1]

if header == "This is a valid save file":
    ### Load game data from file
    cube.setPosition(eval(loadFile.readline()))
    cube.setOrientation(eval(loadFile.readline()))

### Close save file
loadFile.close()

@10nas: I don’t see any problem in your script, I just tested it and it does his job. Try to explain more precisely your problem.

please can you publish this script with two object ???thx

Clasic save-load:
http://www.4shared.com/file/10391893/e8b04e1a/saveload.html

PLEASE!!! can somebody publish .blend file with examle of save and load two objects???
edit-sry i have it