Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Zylann | |
Old Version | Published before Godot 3 was released. |
I have a GDScript class/script, for example this one:
class LevelAchievements:
var best_time = -1.0
var collected_orbs = 0
var death_count = -1
var completed_count = 0
var data = LevelAchievements.new()
In order to write it inside a savegame file, I want to iterate over its members rather than copy/paste everyting on save and load. Usually, this can be done through a form of reflection, but how is it possible to do that in GDScript?
Even better, is there a whay to query which members have export
, and if so what is their type etc?
Not that I know of. There’s get_method_list() for functions but that’s all I can see. I use pretty much the save game method described in the manual.
duke_meister | 2016-06-10 00:57
I see there is get_property_list()
, accordint to the doc it should return this:
Return the list of properties as an array of dictionaries, dictionaries contain: name:String, type:int (see TYPE_* enum in globals) and optionally: hint:int (see PROPERTY_HINT_* in globals), hint_string:String, usage:int (see PROPERTY_USAGE_* in globals).
So I tried:
class Foo:
var one = 1
var two = "two"
var three
func _ready():
var obj = Foo.new()
var list = obj.get_property_list()
for d in list:
print("> " + d["name"])
But the result is completely off Oo
> Reference
I tried this instead:
var list = Foo.get_property_list()
for d in list:
print("> " + d["name"])
More weirdness :o
> Reference
> Resource
> resource/path
> resource/name
> Script
> GDScript
> script/source
Either its a bug, or the function is badly named…
Zylann | 2016-06-10 13:00
Yeah i tried that too. I don’t think it’s properties as we know it.
duke_meister | 2016-06-10 15:05
A workaround would be to use var data = inst2dict(obj)
and var obj = dict2inst(data)
, but it gets all properties, including @path and @subpath to indentify the class.
Zylann | 2016-06-11 14:24