-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making
inst_to_dict
and dict_to_inst
recursive
Fixes #6533 Making GDScript inst_to_dict/dict_to_inst utility functions recursive. Adding also a new macro to validate the number of the required arguments.
- Loading branch information
Showing
10 changed files
with
170 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
modules/gdscript/tests/scripts/utility_functions/bar.notest.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extends Resource | ||
|
||
@export var text: String | ||
@export var qux: Resource | ||
|
||
|
||
func _init(p_text = "", p_qux = null): | ||
text = p_text | ||
qux = p_qux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://cqd4xsl30rr7b"] | ||
|
||
[ext_resource type="Script" path="./bar.notest.gd" id="1_bar"] | ||
[ext_resource type="Script" path="./qux.tres" id="1_qux"] | ||
|
||
[resource] | ||
script = ExtResource("1_bar") | ||
text = "lorem ipsum" | ||
qux = ExtResource("1_qux") |
9 changes: 9 additions & 0 deletions
9
modules/gdscript/tests/scripts/utility_functions/foo.notest.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extends Resource | ||
|
||
@export var number: int | ||
@export var bar: Resource | ||
|
||
|
||
func _init(p_number = 0, p_bar = null): | ||
number = p_number | ||
bar = p_bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://cqd4xsl30rr7a"] | ||
|
||
[ext_resource type="Script" path="./foo.notest.gd" id="1_foo"] | ||
[ext_resource type="Script" path="./bar.tres" id="1_bar"] | ||
|
||
[resource] | ||
script = ExtResource("1_foo") | ||
number = 42 | ||
bar = ExtResource("1_bar") |
56 changes: 56 additions & 0 deletions
56
modules/gdscript/tests/scripts/utility_functions/inst_to_dict.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
extends Resource | ||
|
||
var foo = preload("./foo.tres") | ||
|
||
|
||
func test(): | ||
var obj:Object = foo | ||
var dict:Dictionary = inst_to_dict(obj) | ||
var dict_ok = true | ||
dict_ok = dict_ok && dict.get("number") == 42 | ||
dict_ok = dict_ok && dict.get("bar") is Resource | ||
if not dict_ok: | ||
printerr("Can't convert instance to dictionary properly") | ||
|
||
dict = inst_to_dict(obj, 1) | ||
dict_ok = true | ||
dict_ok = dict_ok && dict.get("number") == 42 | ||
var bar_dict:Dictionary = dict.get("bar") | ||
dict_ok = dict_ok && bar_dict.text == "lorem ipsum" | ||
dict_ok = dict_ok && bar_dict.get("qux") is Resource | ||
if not dict_ok: | ||
printerr("Can't convert instance to dictionary properly") | ||
|
||
var inst = dict_to_inst(dict, 1) | ||
var equals = true | ||
equals = equals && foo.number == inst.number | ||
equals = equals && foo.bar.text == inst.bar.text | ||
equals = equals && inst.bar.qux is Resource | ||
if not equals: | ||
printerr("Can't revert from instance to dictionary properly") | ||
|
||
dict = inst_to_dict(obj, 2) | ||
print(dict.keys()) | ||
print(dict.values()) | ||
|
||
inst = dict_to_inst(dict, 2) | ||
equals = true | ||
equals = equals && foo.number == inst.number | ||
equals = equals && foo.bar.text == inst.bar.text | ||
equals = equals && foo.bar.qux.decimal == inst.bar.qux.decimal | ||
if not equals: | ||
printerr("Can't revert from instance to dictionary properly") | ||
|
||
dict = inst_to_dict(obj, -1) | ||
print(dict.keys()) | ||
print(dict.values()) | ||
|
||
inst = dict_to_inst(dict, -1) | ||
equals = true | ||
equals = equals && foo.number == inst.number | ||
equals = equals && foo.bar.text == inst.bar.text | ||
equals = equals && foo.bar.qux.decimal == inst.bar.qux.decimal | ||
if not equals: | ||
printerr("Can't revert from instance to dictionary properly") | ||
|
||
print('ok') |
6 changes: 6 additions & 0 deletions
6
modules/gdscript/tests/scripts/utility_functions/inst_to_dict.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
GDTEST_OK | ||
["@subpath", "@path", &"number", &"bar"] | ||
[^"", "res://utility_functions/foo.notest.gd", 42, { "@subpath": ^"", "@path": "res://utility_functions/bar.notest.gd", &"text": "lorem ipsum", &"qux": { "@subpath": ^"", "@path": "res://utility_functions/qux.notest.gd", &"decimal": 0.5 } }] | ||
["@subpath", "@path", &"number", &"bar"] | ||
[^"", "res://utility_functions/foo.notest.gd", 42, { "@subpath": ^"", "@path": "res://utility_functions/bar.notest.gd", &"text": "lorem ipsum", &"qux": { "@subpath": ^"", "@path": "res://utility_functions/qux.notest.gd", &"decimal": 0.5 } }] | ||
ok |
7 changes: 7 additions & 0 deletions
7
modules/gdscript/tests/scripts/utility_functions/qux.notest.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
extends Resource | ||
|
||
@export var decimal: float | ||
|
||
|
||
func _init(p_decimal = ""): | ||
decimal = p_decimal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://cqd4xsl30rr7c"] | ||
|
||
[ext_resource type="Script" path="./qux.notest.gd" id="1_qux"] | ||
|
||
[resource] | ||
script = ExtResource("1_qux") | ||
decimal = 0.5 |