From 0e6aa6fc3879c4c1c37bc598a83a44cb5cddfe85 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 24 Feb 2023 21:39:10 -0300 Subject: GDScript: Initialize all defaults beforehand in implicit constructor Set all the default values for typed variables before actually trying to initialize them, including `@onready` ones. This ensures that if validated calls are being used there will be a value of the correct type, even if the resolution is done out of order or deferred because of `@onready`. --- .../runtime/features/default_set_beforehand.gd | 20 ++++++++++++++++++++ .../runtime/features/default_set_beforehand.out | 2 ++ 2 files changed, 22 insertions(+) create mode 100644 modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.out (limited to 'modules/gdscript/tests') diff --git a/modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.gd b/modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.gd new file mode 100644 index 0000000000..03278e453f --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.gd @@ -0,0 +1,20 @@ +extends Node + +@onready var later_inferred := [1] +@onready var later_static : Array +@onready var later_static_with_init : Array = [1] +@onready var later_untyped = [1] + +func test(): + assert(typeof(later_inferred) == TYPE_ARRAY) + assert(later_inferred.size() == 0) + + assert(typeof(later_static) == TYPE_ARRAY) + assert(later_static.size() == 0) + + assert(typeof(later_static_with_init) == TYPE_ARRAY) + assert(later_static_with_init.size() == 0) + + assert(typeof(later_untyped) == TYPE_NIL) + + print("ok") diff --git a/modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.out b/modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.out new file mode 100644 index 0000000000..1b47ed10dc --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.out @@ -0,0 +1,2 @@ +GDTEST_OK +ok -- cgit v1.2.3