如果我想在 Godot 中序列化一个数组,我可以这样做:
var a1 = [1,2,3]
# save
var file = File.new()
file.open("a.sav",File.WRITE)
file.store_var(a1,true)
file.close()
# load
file.open("a.sav",File.READ)
var a2 = file.get_var(true)
file.close()
print(a1)
print(a2)
输出(按预期工作):
[1,3]
[1,3]
但是如果我想序列化一个对象,比如 A.gd 中的这个类:
class_name A
var v = 0
相同的测试,使用 A
的实例:
# instance
var a1 = A.new()
a1.v = 10
# save
var file = File.new()
file.open("a.sav",File.READ)
var a2 = file.get_var(true)
file.close()
print(a1.v)
print(a2.v)
输出:
10
错误(在第 print(a2.v)
行):
Invalid get index 'v' (on base: 'previously freed instance').
来自在线文档:
void store_var(value: Variant,full_objects: bool = false)
Stores any Variant value in the file. If full_objects is true,encoding objects is allowed (and can potentially include code).
Variant get_var(allow_objects: bool = false) const
Returns the next Variant value from the file. If allow_objects is true,decoding objects is allowed.
Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
它不应该与 full_objects=true
一起使用吗?不然这个参数有什么用?
我的类包含许多数组和其他东西。我猜 Godot 会处理这种基本的序列化功能(当然,开发人员通常必须一次性保存复杂的数据),所以,也许我只是没有做我应该做的事情。
有什么想法吗?