godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.27k stars 21.04k forks source link

`inst2dict` does not work with objects having no script on them #30077

Open Zylann opened 5 years ago

Zylann commented 5 years ago

Godot 3.1.1

The following does not work:

var sprite = Sprite.new()
var d = inst2dict(sprite)

Error:

Error calling built-in function 'inst2dict': Not a script with an instance

Is there a reason why the object must have a script on it? Either way, if it's intented it should also be documented.

Xrayez commented 4 years ago

Based on my findings, it does assume that an object has GDScript attached specifically. It uses internal GDScriptInstance as an interface to an object with the script, traversing all script members.

Also it's difficult to tell what kind of properties need to be serialized if it's not based on a script. The question is whether inst2dict should accept an optional array of properties that you're interested in serializing, alongside script properties of course.

inst2dict could also be generalized to handle any object in the future indeed, which doesn't have to be based on a script: https://github.com/godotengine/godot/issues/6533#issuecomment-320896833:

And also they should could be used for all of script languages instead of GDScript only.

Zylann commented 4 years ago

Also it's difficult to tell what kind of properties need to be serialized if it's not based on a script.

Same as with a script I assume? Properties having PROPERTY_HINT_STORAGE. Note: export == PROPERTY_HINT_STORAGE | PROPERTY_HINT_EDITOR

Xrayez commented 4 years ago

I should also note that inst2dict serializes both exported and regular properties.

Properties having PROPERTY_HINT_STORAGE

Is it possible to override this for built-in properties then? I've seen _get_property_list() is used for that but I'm not sure.

I'm also not sure if a user would want to automatically serialize built-in properties along with script properties by default.

Zylann commented 4 years ago

I personally have no real use for this at the moment so I'd be fine if inst2dict documentation covers all those cases, because right now there are inconsistent exceptions like this one. If someone wants to use it on non-script instances we'd have to figure it out.

KoBeWi commented 3 years ago

Still valid in dfed88b

MewPurPur commented 2 years ago

Still valid today: the below throws an error.

var sprite = Sprite2D.new()
print(inst_to_dict(sprite))
pafuent commented 3 weeks ago

Since 4.0 there is a Note in inst_to_dict docs that says:

Note: Cannot be used to serialize objects with built-in scripts attached or objects allocated within built-in scripts.

If there is no intention to keep this issue open for 3.x maybe it could be closed.