diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 7b9aa70686a..9338a2416f2 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -205,8 +205,13 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco Variant GDScript::_new(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { /* STEP 1, CREATE */ + if (!valid && shallow && !reloading && is_root_script()) { + reload(true); + } + if (!valid) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + ERR_FAIL_COND_V_MSG(!valid, Variant(), "Can't instantiate invalid script."); + r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL; return Variant(); } diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 9bb39aac0f7..4785ee56d09 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -63,6 +63,7 @@ class GDScript : public Script { bool tool = false; bool valid = false; bool reloading = false; + bool shallow = true; struct MemberInfo { int index = 0; diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index eebf2826336..c1b7232c57c 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -2645,6 +2645,7 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP parsing_classes.insert(p_script); + p_script->shallow = false; p_script->clearing = true; p_script->native = Ref();