convert a text(str) in a dictionary

hello.

I’m doing some experiments to make a save game

using bge.logic.saveGlobalDict ()
says an error: “not marshallable’d object” (or something)

while saving a dictionary as string in a file text (. txt), it seems all right.
(so the script is very simple)

the problem is the “load”
how do I (re-)convert the text into a dictionary?

eval(dictionnary)

hi dberube4

i work on your very good script as base :wink:

is just a bit too complex to me , i want try to solve the issue of children , and mayble also for the instance(this seem a big mess)

now try your solution thanks

something wrong:


def load(cn):
    
    
        
    path=bge.logic.expandPath('//')
    f = open((path + "saveGame.txt"),"r")
    p = f.evaluate()
    print(p)

ERROR:
“obj(f) has not attribute “evaluate””

It does not work like that:

Try this:


def load(cn):                        
    path=bge.logic.expandPath('//')     
    f = open((path + "saveGame.txt"),"r")     
    p = eval(f)     
    print(p)

The eval function is used to evaluate valid Python expressions which are stored in a string.

see: http://www.swaroopch.com/notes/Python_en:More

yes , eval , not “evaluate”…

but , not see f as string

ERROR:
TypeError : eval() must be a string, bytes or object

that should be a string:


import bge

def save(cn):
    
    scene = bge.logic.getCurrentScene()
    
    saveData = {}
    for ob in scene.objects:
        
        dProp = {}
        for prop in ob.getPropertyNames():
            dProp[prop] = ob[prop]
        
        dGenerics={}
        dGenerics["pos"]        = ob.worldPosition.copy()
        dGenerics["ori"]        = ob.worldOrientation.copy()
        dGenerics["propList"]   = dProp
    
        saveData[ob] = dGenerics
        
        
        
    path=bge.logic.expandPath('//')
    f = open((path + "saveGame.txt"), "w")
    f.write(str(saveData))
    f.close()
        
    
    
    GD=saveData
    print("------------------")
    print("the saved item is:")
    for i in GD :
        print(i)
        print(GD[i])
    
    
    
    
    
        
def load(cn):                        
    path=bge.logic.expandPath('//')     
    f = open((path + "saveGame.txt"),"r")
    p = eval(f)
    
    print(p)

Oops…

I forgot that 'f = open((path + “saveGame.txt”), “r”) ’ does not return the data, it return the file.

To read the file data you must call ‘f.read(n)’ (‘n’ being the maximum of character that can be loaded).

ok! cool, now I see! (conversion completed!)

:wink:

EDIT: conversion only save-> load (but ever text)

mhh… now see the text , but cannot converter it in a dictionary …


def load(cn):                        
    path=bge.logic.expandPath('//')     
    f = open((path + "saveGame.txt"),"r")
    f = f.read(10000)
    print("-------------------------------")
    print(f)                     # this is all right
    
    p = eval(f)                  # this give error ( "Plane is not definited" )
    

Seems like the dictionary is not evaluated correctly. The dictionnary may be malformed.

I knew that could append. Can you try this one:

def load(cn):  
    import ast       
                
    path=bge.logic.expandPath('//')          
    f = open((path + "saveGame.txt"),"r")    
    f = f.read(10000)     
    print("-------------------------------")     
    print(f)                     # this is all right          
    p = ast.literal_eval(f)                  # this give error ( "Plane is not definited" )

if it still doesn’t work:
use ‘f.write(repr(saveData))’ instead of ‘f.write(str(saveData))’ in your save script.

unfortunately does not work, I tried also with repr ()

yet, the text seems right …

something with tuple() ? can be?

I ran a small test with a script like yours.

Because of the vector and all the different data type, I concluded that it is impossible to convert back the dictionary .

The module pickle is the best solution.

the first piece is a print is of “save” when is again a dictionary

the second is the reload version in a text form

not there any strange symbols , all identical!

Attachments


ah…

using pickle after cleaning all Vector, Matrix , and all?

uh , I see the potential error

hemi

and all object is not in to “” o.o
for thath say which Plane is not definited (maybe)

oh, yes!!! post for security , conversion(seem) accomplished!

thanks dberube4 !!


import bge
from mathutils import *


def save(cn):
    if cn.sensors["S"].status==1:
        
        scene = bge.logic.getCurrentScene()
        
        saveData = {}
        for ob in scene.objects:
            
            dProp = {}
            for prop in ob.getPropertyNames():
                dProp[str(prop)] = ob[str(prop)]
            
            dGenerics={}
            dGenerics["pos"]        = ob.worldPosition.copy()
            dGenerics["ori"]        = ob.worldOrientation.copy()
            dGenerics["propList"]   = dProp
        
            saveData[str(ob)] = dGenerics
            
            
            
        path=bge.logic.expandPath('//')
        f = open((path + "saveGame.txt"), "w")
        f.write(repr(saveData))
        f.close()
            
        
        """
        GD=saveData
        print("------------------")
        print("the saved item is:")
        for i in GD :
            print(i)
            print(GD[i])
        """
        print("________________________________")
        print("______________SAVE(dictionary)__")
        print(saveData)
    
    
    
    
    
        
def load(cn):  
    if cn.sensors["L"].status==1:
                          
        path=bge.logic.expandPath('//')     
        f = open((path + "saveGame.txt"),"r")
        
        f = f.read(10000)
        print("________________________________")
        print("______________LOAD(text)________")
        print(f)
        
        p=eval(f)
        
        print(p)
        
        for i in p:
            print(i)
            print(p[i])

Good job, import mathutils was a good idea. ;D

not only this!
was this line which save the references(??) and not the string

saveData[ob] = dGenerics

saveData[str(ob)] = dGenerics

well, thats was the part too difficult …

as not ? :smiley:

LOAD at work:

default cam not exist …

:wink:

ok , work [solved]