MarcoIT
(MarcoIT)
March 22, 2012, 12:42pm
1
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?
MarcoIT
(MarcoIT)
March 22, 2012, 1:55pm
3
hi dberube4
i work on your very good script as base
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
MarcoIT
(MarcoIT)
March 22, 2012, 2:07pm
4
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””
dberube4
(dberube4)
March 22, 2012, 2:10pm
5
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
MarcoIT
(MarcoIT)
March 22, 2012, 2:49pm
6
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)
dberube4
(dberube4)
March 22, 2012, 3:01pm
7
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).
MarcoIT
(MarcoIT)
March 22, 2012, 3:35pm
8
ok! cool, now I see! (conversion completed!)
EDIT: conversion only save-> load (but ever text)
MarcoIT
(MarcoIT)
March 22, 2012, 3:56pm
9
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" )
dberube4
(dberube4)
March 22, 2012, 4:20pm
10
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.
MarcoIT
(MarcoIT)
March 22, 2012, 4:54pm
11
unfortunately does not work, I tried also with repr ()
yet, the text seems right …
something with tuple() ? can be?
dberube4
(dberube4)
March 22, 2012, 5:09pm
12
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.
MarcoIT
(MarcoIT)
March 22, 2012, 5:11pm
13
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
MarcoIT
(MarcoIT)
March 22, 2012, 5:14pm
14
dberube4:
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.
ah…
using pickle after cleaning all Vector, Matrix , and all?
MarcoIT
(MarcoIT)
March 22, 2012, 5:24pm
15
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)
MarcoIT
(MarcoIT)
March 22, 2012, 5:37pm
16
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])
dberube4
(dberube4)
March 22, 2012, 6:04pm
17
Good job, import mathutils was a good idea. ;D
MarcoIT
(MarcoIT)
March 22, 2012, 6:13pm
18
not only this!
was this line which save the references(??) and not the string
saveData[ob] = dGenerics
saveData[str(ob)] = dGenerics
MarcoIT
(MarcoIT)
March 22, 2012, 6:30pm
19
well, thats was the part too difficult …
as not ?
LOAD at work:
default cam not exist …