Python Blender游戏引擎:_pickle.PicklingError错误:无法pickle<class'\uu main.'a class'>:在\uu main上的属性查找“a class”失败

2024-09-28 03:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

import pickle

class NoClass():
    def __init__(self, name, level, cls, time_played):
        self.name = name
        self.level = level
        self.cls = cls
        self.time_played = time_played

def Write_char_file(registered_username):
    avatar = NoClass('',1,'',0) #--------i am trying to pickle and write this
    a = str('Characters\%s.txt' % registered_username) #---- the saving file
    f = open(a, 'wb')
    f.write(pickle.dumps(avatar))
    f.close()

def Asign_to_slot(char_lst):
    pass

Asign_to_slot(Write_char_file('my_name'))

When trying to run this in bge it raises that error BUT when i run it with python IDLE there isn't a problem and i manage to write the pickled class in the file though i know classes usually can't be pickled then i even manage to open the file, unpickle it and print the class' attributes

"_pickle.PicklingError: Can't pickle <class '__main__.NoClass'>: attribute lookup NoClass on __main__ failed"

Tags: andthetonameselftimedeflevel
1条回答
网友
1楼 · 发布于 2024-09-28 03:25:22

由于这在blender中运行时有效,并且只在游戏引擎中出现故障,因此我假设这与游戏引擎python绑定中进行的优化有关。你知道吗

遵循handling stateful objects示例并添加自定义__getstate__也会失败,这表明游戏引擎可能实现自定义^{}以仅提供最小的功能集。你知道吗

解决方案似乎是直接对对象__dict__进行酸洗,然后还可以取消对新实例__dict__的酸洗。你知道吗

f.write(pickle.dumps(avatar.__dict__))

相关问题 更多 >

    热门问题