Blender Game Engine Python Help

Hey guys, I need help on a python script I’m working on. I want my script to detect all objects with a common property. So this is what I do:


g = GameLogic
cont = g.getCurrentController()
 
objs = cont.getCurrentScene().getObjectList()

Ok, now what do I do? In the object List I want to find all the objects with a common property.
Now this is going to be used in a save/load script. So, when I get all the objects with the common property I want to write them in a file, by order of the names of the objects with the property name. EX: obj1 goes first and then obj2. So something like this:


g = GameLogic
cont = g.getCurrentController()
 
objs = cont.getCurrentScene().getObjectList()
 
cp = objs.getPropertyNames() == "commonProperty"
#I know it's not how you really do it, it's just an example
sg = open("TestFile.txt", "w")
 
sg.write (str(cp.byObjectName))
#again I know it's not how you really do it, but it's what I mean.

Can someone help me to make the real script and how would I make the load script?

Please help!

Con some help me, I need to know how to detect all common properties in the scene. Or how do I make a full save/load system that saves everything in the scene.


for obj in objs:
     if obj.property=="wanted value":
         # put code here

cool, thanks, but can you explain this to me please?

A “for in” would be the right way to go, but I think he’s looking for the name of the property itself, not the string that it holds (because maybe it’s not a string in the first place).

@Linkxgl

You need to parse over your objects like this:


for obj in objs:
    if hasattr(obj, "property_name"):
        # save code relating to that object goes here

As for the actual save/load code: I recommend you use a data structure (list or a dictionary) to save all object attributes, and then simply pickle the dictionary into a file.

That way you can save everything with cPickle.dump(data_struct, file), and load everything with cPickle.load(file).

You can see the use of pickle demonstrated in the following thread:
http://blenderartists.org/forum/showthread.php?t=153028

for obj in objs:
if hasattr(obj, “property_name”):
# save code relating to that object goes here

As for the actual save/load code: I recommend you use a data structure (list or a dictionary) to save all object attributes, and then simply pickle the dictionary into a file.

That way you can save everything with cPickle.dump(data_struct, file), and load everything with cPickle.load(file).

Thanks!
So, the code above finds all the objects in the scene, with the name of the property? How would I save that? I mean the value of each object with the common property? Do you know what I mean? I want to save all the common properties VALUE, not the name.
BTW that cPickle is great!

You could do it this way:



dict_savedata = {} # create a new temporary dictionary

for obj in objs:
    if hasattr(obj, "property_name"):
        dict_savedata[obj.getName()] = obj.property_name

save_file = open("file_name.extension", "wb")

cPickle.dump(dict_savedata, save_file)

save_file.close()


And then you can load it all like this:



save_file = open("file_name.extension", "rb")

dict_loaddata = cPickle.load(save_file)

save_file.close()

# So let's say that one of my objects was the default cube, and it had the prop.
# I could now access the value of it's saved prop like this:
print dict_loaddata["OBCube"]


The cPickle module, and the dictionary data structure make it all very convenient.

Any other questions: feel free to ask.

My last question. When I load the data, how can I do multiple objects?
ps: This might not be my last question :slight_smile:

Thank you very much!

What do you mean by “do”?

PS: All the objects that have the “common property” will have the value of that property saved in the dictionary, and the dictionary as a whole is saved to the actual file.

Then on load, that whole dictionary is loaded again, so if you had multiple objects in the game that carried the “common property” when you saved, you’ll have multiple values for each of those objects in the loaded dictionary.

I mean, how do I list multiple objects for example:
[“OBCube”][“OBCube1”]
[“OBCube” “OBCube1”]
[“OBCube”,“OBCube1”]

How I list them?
NVM, I figured it out, but each time I load it, nothing happens. It just prints all the Objects, how do I get it to actually change the common property on each individual object?

Thanks!

It’s similar to how you saved:



for obj in objs:
    if hasattr(obj, "property_name"):
        obj.property_name = dict_loaddata[obj.getName()]


ah, thanks alot, I think that’s it for me asking questions, thanks a bunch! :smiley:

what if the objects in the scene are not the same ones?
fixes for that could get really complex, but an easy way out would be:


obs=GameLogic.getCurrentScene().getObjectList()
names=
for i in obs:
    if hasattr(i,"ATTR_IN_QUESTION"):
        names[i.getName()]=i
#now we know all the names...
for i in loaded_dictionary.keys(): 
    if i in names.keys():
        ob=names[i]
        ob.ATTR_IN_QUESTION=loaded_dictionary[i]

where ATTR_IN_QUESTION is you property
and loaded_dictionary is the dictionary that was unpickled.
this will avoid problems if one of the objects doesn’t exist anymore.