Save and Load script v4.3

I have read that adriansnetlis problem is solved.
Best way to debug this script is by removing all save properties from the objects until you find the object you try to save that gives errors.

I have not tested this, but it should work i guess. try this:

for obj in [obj for obj in scene.objects if 'save' in obj]:        
        obj_data                = {}
       
        if 'Text' in obj:
            
            properties          = {prop: obj[prop] for prop in obj.getPropertyNames()}
            
            obj_data['name']        = obj.name 
            obj_data['properties']  = properties
            obj_data['save']        = obj['save']   


            objects_array.append(obj_data)
            
        else:
        
            print('- ' + obj.name + ' (' + str(obj['save']) + ')')
        
            position    = list(obj.worldPosition)
            orientation = 
[list(vector) for vector in obj.worldOrientation]
            properties  = {prop: obj[prop] for prop in obj.getPropertyNames()}
      
            obj_data['position']    = position
            obj_data['orientation'] = orientation          
            obj_data['name']        = obj.name      
            obj_data['properties']  = properties
            obj_data['save']        = obj['save']
        
            objects_array.append(obj_data)

Keep in mind this is only the save part, you need to adjust the loading aswell, else it will throw out errors because you arent saving position and orientation of those objects, so the script cant set them.

Oh cool. I havenā€™t had a chance to mess with it yet. Iā€™m working on an easy bolt on inventory using mostly logic bricks and your script. :slight_smile:

Thanks again.

Hi! Is it possible to saw objects linear velocity somehow?

obj.localLinearVelocity

You can add thiis, to an extra expansion of the script.
Look at the save part, make an extra varable.

Dont forget to change to load part aswell

What is lovalLinearVelocity? Is it list? If not that can it be saved to .bgeconf?

yes: http://www.tutorialsforblender3d.com/BGE_Python/Game_Logic/GameObject/GameObject_localLinearVelocity.html

list(obj.localLinearVelocity)

Should work.

You should implement it in the default code so evryone can use it easily as it is very, very useful;)

Hehe canā€™t figure it out? :wink:

ok give me a few min iā€™ll build it in.

Ok, done.
I uploaded v2.1, and it now saves velocities aswell.

#Edit:
I have seen you have asked for angular velocity aswell, i added that one now aswell.

OK! This is amazing, really great, cotax!:slight_smile:

Are you aware that you cannot save ā€œstaticā€ objects using this method? Any objects that were not instantiated during the game cannot be re-loaded. Iā€™ve implemented this support in my resource, if you would like to take a look.

Are you aware that you cannot save ā€œstaticā€ objects using this method? Any objects that were not instantiated during the game cannot be re-loaded

Hmm what do you mean with ā€œstaticā€? As in trough libload? if thatā€™s the case, i donā€™t know because i donā€™t use it.

Youā€™re clearing the scene objects before loading them by spawning new objects. If the objects that you delete were not added using addObject or the add object actuator (i.e they were already in the active layer, from blender) then you cannot use addObject to add them

Youā€™re clearing the scene objects before loading them by spawning new objects. If the objects that you delete were not added using addObject or the add object actuator (i.e they were already in the active layer, from blender) then you cannot use addObject to add them

Ah is that what you mean, i thought something else. It works agoose, thats why i making use of a boolean.
True is inactive (spawned objects)
False is object that are in the main layer.

It is all working, so please donā€™t say that something isnā€™t working while you didnā€™t test it!
I only load data from the main layer, and i spawn new objects from the inactive layer.

And why would you kill something from the main layer, if you want to ise it lateron?
so those objects should be added already instead of being in the main layer.

I tested it, and enabling ā€œsaveā€ caused an error. I suggest that this shouldnā€™t happen, and in order to prevent it you should take care to consider how instances are managed. If the user deletes an object in game, then the script will fail later on, which instead should probably display an error.

In addition, partially due to the naming of the property - ā€œsaveā€, I skimmed through the code and overlooked the saving object main layer objects. I would recommend, if you decide to keep the property, using two properties - one called ā€œsaveā€ to mark the object as saveable, and one called ā€œis_instanceā€, to avoid confusion. You can, however, infer this when you load objects back in - if theyā€™re on a hidden layer, then any instances should first be deleted before loading.

I tested it, and enabling ā€œsaveā€ caused an error.

So you tested it with an object in the main layer? yup you will get an error because active(main layer) objects needs ā€œsaveā€ to be false.

Tell me in what senario would you like to delete objects that are in/from the main layer?
Just donā€™t kill em, and all data will be reloaded upon loading the game.

Killable data should be in an inactive layer, with ā€œsaveā€ true.
they get spawned into the main layer, can be killed and respawned when the user whants.

If the user deletes an object in game, then the script will fail later on,

Only if the object is from the main layer with ā€œsaveā€ True. And this is something you want to avoid.

Indeed, I suggest that this shouldnā€™t happen, and instances should be detected by the script without needed explicit saving. This can be done by checking if there is an inactive object with the same name. In addition, the name ā€œsaveā€ implies the object should be saved, with the value toggling this state, whereas you use the value of ā€œsaveā€ to indicate whether is is an instance or not. Perhaps splitting this into two parameters (if you want to manually indicate that this object is an instance) would avoid this confusion.

Generally speaking, assume users make mistakes, and display an error and catch this exception.

Generally speaking, assume users make mistakes, and display an error and catch this exception.

True, it is a bit cleaner to catch the error. but i dont hve the time to rewrite it, so for now it stay like this.
Thanks for your advice.

Thanks so much for this script and readme file!

I was about to suggest this - saving the local space state will ensure that parenting doesnā€™t cause issues if you set objects in the wrong order. Try replacing worldPosition with localPosition (some for orientation, velocity)