globalDict save load multiple object property

if i have one object i go like this:

save:


import bge


cont = bge.logic.getCurrentController()


own = cont.owner


bge.logic.globalDict['prop']  = own["prop"]
bge.logic.saveGlobalDict()

load:


import bge


cont = bge.logic.getCurrentController()


own = cont.owner


bge.logic.loadGlobalDict()
own["prop"] = bge.logic.globalDict['prop']




but how i can save-load multiple object property using: for prop in obj.getPropertyNames()}

you already looked into my save and load i suggest to look again to find the right solution.

my python scripting still weak and in your save load you have position, orientation, scale… all this got me confused i just need properties. just a simple and few line script

This should drive you on the correct answer


for property_name in owner.getPropertyNames():

    value = owner[property_name]

Just store the properties in the global dict (you have a name, and a value doing this), then save the dict like you did. Done.

Chances are you may want to store others properties from other objects. Best way would be:


localDict = bge.logic.globalDict[owner.name] = {}

localDict["key"] = "value"

This way you can store properties on a rather “per object” basis (warning: some objects can end up with the same name).

The problem then becomes extracting the values out of the global dict. Know that you can parse the value of a dict doing:


for key, value in dict.items():

    print(key, value)

in the way you have it

to save:


bge.logic.globalDict['prop']  = {prop: own[prop] for prop in own.getPropertyNames()}

to load


        properties = bge.logic.globalDict['prop']


        for prop_name, prop_value in properties.items():
          
            own[prop_name] = prop_value

as said you just needed to look for it :wink:

sorry i still don’t get it, how make it save and load all scene objects property not only the owner?

save:


import bge


cont = bge.logic.getCurrentController()


own = cont.owner


bge.logic.globalDict['prop']  = {prop: own[prop] for prop in own.getPropertyNames()}
bge.logic.saveGlobalDict()


load:


import bge


cont = bge.logic.getCurrentController()


own = cont.owner


bge.logic.loadGlobalDict()


properties = bge.logic.globalDict['prop']


for prop_name, prop_value in properties.items():
    own[prop_name] = prop_value

Don’t have so much time today, but in general:

You need to grab the scene
then you need to loop trough the objects (for obj in scene.objects)
then you execute the property code
then you append that data to a list/dict
put that list into the gd

with loading almost the same thing
loop trough the scene objects
grab the object data from the gd and restore the objects property

is it like this for the save?:


import bge


cont = bge.logic.getCurrentController()


scene = bge.logic.getCurrentScene()


for obj in scene.objects:
        
    obj_data = {}
    
    obj_data['properties']  = {prop: obj[prop] for prop in obj.getPropertyNames()}
    
    bge.logic.globalDict['prop']  = {prop: obj[prop] for prop in obj.getPropertyNames()}
    
    bge.logic.saveGlobalDict(


import bge

# Not used right now but why not
controller = bge.logic.getCurrentController()

# We will need this indeed
scene = bge.logic.getCurrentScene()

# The correct way to iter over the objects in the scene :)
for obj in scene.objects:
    
    # Ok, so you creating a dict to store your props, cool
    obj_data = {}
    
    # Now, you are creating a new dictionnary per object, but they all end up in a 'property' field
    # Maybe use `obj.name` instead of `'properties'` ?
    obj_data['properties']  = {prop: obj[prop] for prop in obj.getPropertyNames()}
    
    # Hmm, why recreating a dict there ?
    # You just have to put your `obj_data` into the global dict, like in a `'data'` field ?
    bge.logic.globalDict['prop']  = {prop: obj[prop] for prop in obj.getPropertyNames()}
    
    # Ok this should be good
    bge.logic.saveGlobalDict()

It seems like this works:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner


globalDict = bge.logic.globalDict


oneKey = cont.sensors['ONEKEY']
twoKey = cont.sensors['TWOKEY']


if oneKey.positive:
    property = {}
    for propName1 in own.getPropertyNames():
        property[propName1] = own[propName1]
        globalDict[str(own)] = [property]        
    bge.logic.saveGlobalDict()                
                
if twoKey.positive:
    bge.logic.loadGlobalDict()
    for names in globalDict:
        objList = globalDict[names]
        for propName2 in objList[0]:
            own[propName2] = objList[0][propName2]

example - save_load_property.blend (466 KB)

thank you for the help, those this save multiple objects property?

  • no problem, always happy to help.

No, but i really wonder why you want to reinvent the wheel, i wrote a save/load already.
look at the save part. I’m not going to keep writing the same scripts over and over.

In my script, look at def save(cont): and nothing else just that part.

Line 28 to 60 is all you need, you can remove anything you like between the lines 45 to 57
add the bge.logic.saveGlobalDict() on the bottom of the script. And you can change the GD at the bottom to: GD[‘saved_data’] = objects_array, also remove the scene for loop, and adjust scene = into getCurrentScene().

So with that said and executed you get this:


    GD              = bge.logic.globalDict        
    scene           = bge.logic.getCurrentScene()
    
    #list where we append the data per object this list is our container-
    #that at the end holds all objects data
    objects_array   = [] 
    
    #loop trough the scene objects that got a property called save 
    for obj in [obj for obj in scene.objects if 'save' in obj]:
        
        # create a dict where we put all data that we want to save per object
        obj_data                = {} 


        obj_data['name']        = obj.name
        obj_data['properties']  = {prop: obj[prop] for prop in obj.getPropertyNames()}
        
        #put the data we collected from the object into our container       
        objects_array.append(obj_data)
    
    #put the container that holds all the objects data into the globalDict
    GD['saved_data'] = objects_array
    
    #save the GD
    bge.logic.saveGlobalDict()

And to be honest, saving is the easy part, loading is the hard part, but that is up to you now.

As said before, if you don’t have the skill to make a save and load, just grab one out of forums resource section, they aren’t there for nothing.

I completely understand that you want to learn how to make it on your own, but you should increase your skill level first so you understand at least the basics. I know it’s not so fun, but trust me it will help you in the long run.

  • Really? The wheel does not have to be invented … Ah the basis … ah the final product. At me basically all is already ready for an end-product, time for possible claims on a copyright (fraud with cheese ~ 2 years) has not expired - at us the

Law not for simple people.And save / load can be done and the lesson of Thatimster in the text file - it’s enough for me.
I foresaw this… (FUI)

He ask to save all objects it’s properties, your script only saves/loads 1 object.

Then again my posts are meant for richjhack not for you, (everybody may use the scripts that i post) And he ask me if he had done it right, he also says that his skill level is not that great, so i just suggested to increase it a bit more, no offense intended, just friendly advice.

so what is this whining about the law and copyright? what has that to do with me helping richjhack? And what has cheese to do with this? i’m confused.

Thatimster has some tutorials indeed, if they are good, i have no clue, haven’t used a single tutorial from him. But i know that he’s a good coder.
and if his tutorial is good enough for you, that’s great, but richjhack didn’t ask for something that is good for you, he asked if he had it done the right way.

WKnight02 gave him the right answer, but that answer is not complete, it missed a list to put everything into before adding it into the GD(look at my post).

thanks again for your help.
i think, Nick Manchul meant that creating our own scripts is better than grabbing scripts from resources because some scripts in resources have copyright and limited use license, Nick Manchul had issue with license before and he post this thread speaking about it:

Np.

I know the thread, but that has nothing to do with this topic.
Agoose/monster/me/others all got save and load systems inhere, without a license attached, (well mine got some rules to follow, but that does not stop you from using it in your game, it’s just there to stop other people to make money of my work by selling the script) and i even told you to grab mine, giving you the permission to use it.

Creating your own scripts is indeed better, i do that as well, i use no other persons scripts, this will increase your skill level as well due to you need to find out a lot on your own. But if you don’t have the skill level to build one on your own and you need that script then i would just grab one from the resources, as said we post them to be used/learned from not to collect dust ;).

Nick, you already know how to save the value of one property. The method you use allows you to save several different properties already.

You established a relationship between property name and storage key. This means you know the property of the object within a scene and know where to store it. The other way around: you know the storage so you know where to apply the value to.

As is now you have no relationship to the object and/or the scene. This means the context of your code is for a specific object of a specific scene.

You can introduce such an relationship that allows you to determine the scene and the object. Imagine you do that manually and you have a box of values. How you know what value should be applied to what scene/object/property?

There are several options. Basically you need a unique key.

Flat structure


{ "Scene.Cube.properties.prop": 1.234
, "Scene.Camera.properties.prop": "i'm a string value"}

Hirarchie


"Scenes": {
    "Scene": {
        "Cube": {
            "properties": {
                "prop" : 1.234,
                "propA": 1234 
            }
        }
    }
    , "Overlay": {
        "Menu" : {
            "properties {
            "prop": "i'm a string value" 
        }
    }
}
 

As this code can quickly become nearly impossible to read and understand you might go for a hierarchy of objects with classes. But this requires a deeper understanding of Python than you currently have.

Nick, you already know how to save the value of one property.

  • Yes of course I know how to do it, just know that this code was written by the professional - I used it as a reference.I’m going to the original sourceto ask about copyrights, and then somehow it will not be correct … it was more interesting.
  • ah license, I would say so - FUI. And - link

Thank you for your understanding, criticism and patience. :cool: