diff options
Diffstat (limited to 'modules/gdscript/gdscript.cpp')
-rw-r--r-- | modules/gdscript/gdscript.cpp | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index f238958f25..de56dd5ece 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -49,11 +49,12 @@ #include "core/config/engine.h" #include "core/config/project_settings.h" #include "core/core_constants.h" -#include "core/core_string_names.h" #include "core/io/file_access.h" #include "core/io/file_access_encrypted.h" #include "core/os/os.h" +#include "scene/scene_string_names.h" + #ifdef TOOLS_ENABLED #include "editor/editor_paths.h" #endif @@ -678,6 +679,27 @@ Error GDScript::_static_init() { #ifdef TOOLS_ENABLED +void GDScript::_static_default_init() { + for (const KeyValue<StringName, MemberInfo> &E : static_variables_indices) { + const GDScriptDataType &type = E.value.data_type; + // Only initialize builtin types, which are not expected to be `null`. + if (!type.has_type || type.kind != GDScriptDataType::BUILTIN) { + continue; + } + if (type.builtin_type == Variant::ARRAY && type.has_container_element_type(0)) { + Array default_value; + const GDScriptDataType &element_type = type.get_container_element_type(0); + default_value.set_typed(element_type.builtin_type, element_type.native_type, element_type.script_type); + static_variables.write[E.value.index] = default_value; + } else { + Variant default_value; + Callable::CallError err; + Variant::construct(type.builtin_type, default_value, nullptr, 0, err); + static_variables.write[E.value.index] = default_value; + } + } +} + void GDScript::_save_old_static_data() { old_static_variables_indices = static_variables_indices; old_static_variables = static_variables; @@ -841,6 +863,9 @@ Error GDScript::reload(bool p_keep_state) { #ifdef TOOLS_ENABLED if (can_run && p_keep_state) { _restore_old_static_data(); + } else if (!can_run) { + // Initialize static variables with sane default values even if the constructor isn't called. + _static_default_init(); } #endif @@ -1958,19 +1983,22 @@ int GDScriptInstance::get_method_argument_count(const StringName &p_method, bool return 0; } +void GDScriptInstance::_call_implicit_ready_recursively(GDScript *p_script) { + // Call base class first. + if (p_script->_base) { + _call_implicit_ready_recursively(p_script->_base); + } + if (p_script->implicit_ready) { + Callable::CallError err; + p_script->implicit_ready->call(this, nullptr, 0, err); + } +} + Variant GDScriptInstance::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { GDScript *sptr = script.ptr(); - if (unlikely(p_method == SNAME("_ready"))) { - // Call implicit ready first, including for the super classes. - while (sptr) { - if (sptr->implicit_ready) { - sptr->implicit_ready->call(this, nullptr, 0, r_error); - } - sptr = sptr->_base; - } - - // Reset this back for the regular call. - sptr = script.ptr(); + if (unlikely(p_method == SceneStringName(_ready))) { + // Call implicit ready first, including for the super classes recursively. + _call_implicit_ready_recursively(sptr); } while (sptr) { HashMap<StringName, GDScriptFunction *>::Iterator E = sptr->member_functions.find(p_method); @@ -2011,15 +2039,15 @@ void GDScriptInstance::notification(int p_notification, bool p_reversed) { } String GDScriptInstance::to_string(bool *r_valid) { - if (has_method(CoreStringNames::get_singleton()->_to_string)) { + if (has_method(CoreStringName(_to_string))) { Callable::CallError ce; - Variant ret = callp(CoreStringNames::get_singleton()->_to_string, nullptr, 0, ce); + Variant ret = callp(CoreStringName(_to_string), nullptr, 0, ce); if (ce.error == Callable::CallError::CALL_OK) { if (ret.get_type() != Variant::STRING) { if (r_valid) { *r_valid = false; } - ERR_FAIL_V_MSG(String(), "Wrong type for " + CoreStringNames::get_singleton()->_to_string + ", must be a String."); + ERR_FAIL_V_MSG(String(), "Wrong type for " + CoreStringName(_to_string) + ", must be a String."); } if (r_valid) { *r_valid = true; @@ -2892,7 +2920,7 @@ String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) con return ""; } -void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<String> *r_dependencies, bool p_add_types) { +void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ); ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_path + "'."); @@ -2906,13 +2934,8 @@ void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<S return; } - GDScriptAnalyzer analyzer(&parser); - if (OK != analyzer.analyze()) { - return; - } - for (const String &E : parser.get_dependencies()) { - r_dependencies->push_back(E); + p_dependencies->push_back(E); } } |