diff options
author | George Marques <george@gmarqu.es> | 2021-03-09 12:32:35 -0300 |
---|---|---|
committer | George Marques <george@gmarqu.es> | 2021-03-29 10:45:48 -0300 |
commit | 85e316a5d58a1866ff4c8738f1703b01629abc32 (patch) | |
tree | 5fd740ecc8f89687818f48ca55202cef63402a6a /modules/gdscript/gdscript.cpp | |
parent | 997a8ae9e8fc04a1a0ea222cbd4922a43861a959 (diff) |
Add typed arrays to GDScript
- Use `Array[type]` for type-hints. e.g.:
`var array: Array[int] = [1, 2, 3]`
- Array literals are typed if their storage is typed (variable
asssignment of as argument in function all). Otherwise they are
untyped.
Diffstat (limited to 'modules/gdscript/gdscript.cpp')
-rw-r--r-- | modules/gdscript/gdscript.cpp | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index a129b73c1a..aea86cce54 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -1310,21 +1310,29 @@ bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) { return true; //function exists, call was successful } } else { - if (!member->data_type.is_type(p_value)) { - // Try conversion - Callable::CallError ce; - const Variant *value = &p_value; - Variant converted; - Variant::construct(member->data_type.builtin_type, converted, &value, 1, ce); - if (ce.error == Callable::CallError::CALL_OK) { - members.write[member->index] = converted; - return true; - } else { - return false; + if (member->data_type.has_type) { + if (member->data_type.builtin_type == Variant::ARRAY && member->data_type.has_container_element_type()) { + // Typed array. + if (p_value.get_type() == Variant::ARRAY) { + return VariantInternal::get_array(&members.write[member->index])->typed_assign(p_value); + } else { + return false; + } + } else if (!member->data_type.is_type(p_value)) { + // Try conversion + Callable::CallError ce; + const Variant *value = &p_value; + Variant converted; + Variant::construct(member->data_type.builtin_type, converted, &value, 1, ce); + if (ce.error == Callable::CallError::CALL_OK) { + members.write[member->index] = converted; + return true; + } else { + return false; + } } - } else { - members.write[member->index] = p_value; } + members.write[member->index] = p_value; } return true; } |