godotengine / godot

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

Instances of inner class can't be load back with ConfigFile #10189

Open Geequlim opened 7 years ago

Geequlim commented 7 years ago

Operating system or device - Godot version: Godot 3.0 alpha1 on Windows 10

Issue description:

There is a class named MyClass.gd. Instance of this class can be save and load back with ConfigFile but something wrong with the member subObj which is an Instance of the inner class SubClass.

var name = "boy"
var age = 2
var subObj = SubClass.new()

class SubClass:
    var sex = "male"

Steps to reproduce:

  1. Download the project below
  2. Click "Save" button to save an instance of MyClass with ConfigFile
  3. Click "Load" button to load the saved instance back from the saved file.

Link to minimal example project: ConfigFileBug.zip

bojidar-bg commented 7 years ago

Related to #6533?

Geequlim commented 7 years ago

In fact the ConfigFile doesn't requires the GDScript module. So it would be better if inst2dict and dict2inst could be seprated from GDScript language and make them virtual with different script languages.


Edited

If the inst2dict and dict2inst become a common used functions then we can use them to serialize/unserialize script instances everywhere in the same way.

Calinou commented 4 years ago

@Geequlim Can you still reproduce this bug in Godot 3.2.1 or 3.2.2beta4?

akien-mga commented 4 years ago

Still reproducible in 3.2.3 RC 1.

pafuent commented 1 month ago

This is still reproducible on 4.3 stable. What I noticed is even the properties of SubClass are properly stored in the config file, the problem seems to be that because SubClass is an inner class Godot is not able to properly "identify" the object script and due to that the SubClass type. So when the object is loaded from the file, the properties are filled with the values from the file, but not the type. I tried to see if I was able to identify an issue in VariantWriter and VariantParser which are the classes that ConfigFile uses to serialize data, but due to my lack of knowledge on the relation between Object and scripts I couldn't. What I found is a way to get it working: making subObj typed. That let Godot set the type properly and everything works as expected.

MyClass.gd should be:

var name = "boy"
var age = 2
var subObj:SubClass = SubClass.new()
var anotherObj = preload("AnothorClass.gd").new()

class SubClass:
    var sex = "male"