diff options
author | George Marques <george@gmarqu.es> | 2023-02-24 21:39:10 -0300 |
---|---|---|
committer | George Marques <george@gmarqu.es> | 2023-02-24 22:01:06 -0300 |
commit | 0e6aa6fc3879c4c1c37bc598a83a44cb5cddfe85 (patch) | |
tree | d58db95f494f6b4654ca4f54ee0e178ab3c8dccf /modules/gdscript/tests/scripts | |
parent | 6296b46008fb8d8e5cb9b60af05fa1ea26b8f600 (diff) |
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`.
Diffstat (limited to 'modules/gdscript/tests/scripts')
-rw-r--r-- | modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.gd | 20 | ||||
-rw-r--r-- | modules/gdscript/tests/scripts/runtime/features/default_set_beforehand.out | 2 |
2 files changed, 22 insertions, 0 deletions
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 |