permanently add new objects in a scene during run-time

Hi,
I’m working on an e-learning game where the user has the option to add objects in BGE like test-tubes, beakers etc and if the mesh is too heavy then optimize it. I have so far implemented optimization of a mesh already present on the 3D viewport in Blender (calling Poly Reducer which optimizes the mesh in Blender, and then restarting the scene through an Actuator so that the changes are seen in the BGE).

However, because I restart the game engine, I lose the objects (kept on other layers) that I add through the add object actuator. :mad: Is there a way to permanently add objects in a scene (in case of add overlay scene, I cannot manipulate the objects so that’s not an option for me)

Please help!

I think if you want to keep changes made in the game engine when you restart it you have to construct a save file for instance:

import GameLogic as g
import cPickle
sceneobjs = g.getCurrentScene().getObjectList()
testtube = sceneobjs["OBTest_Tube"]

save_file = open("Save File", "w") #This creates a file on your computer called Save File.
testtubedata = [testtube.getPosition(), testtube.getOrientation()] #This will only set the position and rotation of objects already in the scene at startup, but you get the idea, I hope atleast.
cPickle.dump(testtubedata, save_file) #This saves it into the file in a pickled format so it can be extracted as complex python data. 
save_file.close()

I haven’t got time to give you an extractor script now, but I can later if you need one.

will this work surely? If you do not have time to write the extractor script then may be if you can give me link to some tutorial for the same.

Thanks for the help!

This is a extractor script to match the other one, you would want to run this when loading a saved game, and run the other when saving the game.

import GameLogic as g
import cPickle
sceneobjs = g.getCurrentScene().getObjectList()
testtube = sceneobjs["OBTest_Tube"]
save_file = open("Save_File", "r")#It's an "r" this time because we are reading from the file
testtubedata = cPickle.load(save_file) #Read the first pickled object in the file
testtube.setPosition(testtubedata[0])
testtube.setOrientation(testtubedata[1])
save_file.close()

Thanks for the help! You made my job easier! This will surely guarantee me an A+ in my project! Thanks again!

No problem =)