diff options
author | George Marques <george@gmarqu.es> | 2019-12-13 12:51:53 -0300 |
---|---|---|
committer | George Marques <george@gmarqu.es> | 2019-12-13 12:51:53 -0300 |
commit | cb887324e6780a05f969de0c7387ac9e8c8cb5c2 (patch) | |
tree | 3832bdcb68e529ca8f7debf9aa30496d300312a5 | |
parent | 475d7f0e5211c981db87fd02176cfa5dab02833e (diff) |
GDScript: Convert values when setting member variables
This allows doing: self.x = 1 even if self.x is declared as float.
-rw-r--r-- | modules/gdscript/gdscript.cpp | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 2f620df8fb..03b51be27f 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -946,18 +946,29 @@ bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) { { const Map<StringName, GDScript::MemberInfo>::Element *E = script->member_indices.find(p_name); if (E) { - if (E->get().setter) { + const GDScript::MemberInfo *member = &E->get(); + if (member->setter) { const Variant *val = &p_value; Variant::CallError err; - call(E->get().setter, &val, 1, err); + call(member->setter, &val, 1, err); if (err.error == Variant::CallError::CALL_OK) { return true; //function exists, call was successful } } else { - if (!E->get().data_type.is_type(p_value)) { - return false; // Type mismatch + if (!member->data_type.is_type(p_value)) { + // Try conversion + Variant::CallError ce; + const Variant *value = &p_value; + Variant converted = Variant::construct(member->data_type.builtin_type, &value, 1, ce); + if (ce.error == Variant::CallError::CALL_OK) { + members.write[member->index] = converted; + return true; + } else { + return false; + } + } else { + members.write[member->index] = p_value; } - members.write[E->get().index] = p_value; } return true; } |