Add Script::inherits_script()

Partial cherry-pick of 5d4dc2d45c.

Co-authored-by: Juan Linietsky <reduzio@gmail.com>
This commit is contained in:
Pedro J. Estébanez 2021-08-02 04:48:17 +02:00
parent 504f47eaec
commit 7426b3fa91
11 changed files with 54 additions and 0 deletions

View file

@ -119,6 +119,8 @@ public:
virtual Ref<Script> get_base_script() const = 0; //for script inheritance virtual Ref<Script> get_base_script() const = 0; //for script inheritance
virtual bool inherits_script(const Ref<Script> &p_script) const = 0;
virtual StringName get_instance_base_type() const = 0; // this may not work in all scripts, will return empty if so virtual StringName get_instance_base_type() const = 0; // this may not work in all scripts, will return empty if so
virtual ScriptInstance *instance_create(Object *p_this) = 0; virtual ScriptInstance *instance_create(Object *p_this) = 0;
virtual PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) { return nullptr; } virtual PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) { return nullptr; }

View file

@ -111,6 +111,11 @@ void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder)
#endif #endif
bool NativeScript::inherits_script(const Ref<Script> &p_script) const {
WARN_PRINT_ONCE("Inheritance needs to be implemented in NativeScript.");
return false;
}
void NativeScript::set_class_name(String p_class_name) { void NativeScript::set_class_name(String p_class_name) {
class_name = p_class_name; class_name = p_class_name;
} }

View file

@ -130,6 +130,8 @@ protected:
public: public:
inline NativeScriptDesc *get_script_desc() const; inline NativeScriptDesc *get_script_desc() const;
bool inherits_script(const Ref<Script> &p_script) const;
void set_class_name(String p_class_name); void set_class_name(String p_class_name);
String get_class_name() const; String get_class_name() const;

View file

@ -136,6 +136,11 @@ bool PluginScript::can_instance() const {
return can; return can;
} }
bool PluginScript::inherits_script(const Ref<Script> &p_script) const {
WARN_PRINT_ONCE("Inheritance needs to be implemented in PluginScript.");
return false;
}
Ref<Script> PluginScript::get_base_script() const { Ref<Script> PluginScript::get_base_script() const {
if (_ref_base_parent.is_valid()) { if (_ref_base_parent.is_valid()) {
return Ref<PluginScript>(_ref_base_parent); return Ref<PluginScript>(_ref_base_parent);

View file

@ -71,6 +71,8 @@ private:
protected: protected:
static void _bind_methods(); static void _bind_methods();
bool inherits_script(const Ref<Script> &p_script) const;
PluginScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Variant::CallError &r_error); PluginScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Variant::CallError &r_error);
Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error); Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error);

View file

@ -815,6 +815,24 @@ Ref<GDScript> GDScript::get_base() const {
return base; return base;
} }
bool GDScript::inherits_script(const Ref<Script> &p_script) const {
Ref<GDScript> gd = p_script;
if (gd.is_null()) {
return false;
}
const GDScript *s = this;
while (s) {
if (s == p_script.ptr()) {
return true;
}
s = s->_base;
}
return false;
}
bool GDScript::has_script_signal(const StringName &p_signal) const { bool GDScript::has_script_signal(const StringName &p_signal) const {
if (_signals.has(p_signal)) { if (_signals.has(p_signal)) {
return true; return true;

View file

@ -146,6 +146,8 @@ protected:
public: public:
virtual bool is_valid() const { return valid; } virtual bool is_valid() const { return valid; }
bool inherits_script(const Ref<Script> &p_script) const;
const Map<StringName, Ref<GDScript>> &get_subclasses() const { return subclasses; } const Map<StringName, Ref<GDScript>> &get_subclasses() const { return subclasses; }
const Map<StringName, Variant> &get_constants() const { return constants; } const Map<StringName, Variant> &get_constants() const { return constants; }
const Set<StringName> &get_members() const { return members; } const Set<StringName> &get_members() const { return members; }

View file

@ -3205,6 +3205,16 @@ void CSharpScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
} }
} }
bool CSharpScript::inherits_script(const Ref<Script> &p_script) const {
Ref<CSharpScript> cs = p_script;
if (cs.is_null()) {
return false;
}
WARN_PRINT_ONCE("Implement CSharpScript::inherits_script and other relevant changes after GH-38063.");
return false;
}
Ref<Script> CSharpScript::get_base_script() const { Ref<Script> CSharpScript::get_base_script() const {
// TODO search in metadata file once we have it, not important any way? // TODO search in metadata file once we have it, not important any way?
return Ref<Script>(); return Ref<Script>();

View file

@ -184,6 +184,8 @@ public:
virtual bool is_tool() const { return tool; } virtual bool is_tool() const { return tool; }
virtual bool is_valid() const { return valid; } virtual bool is_valid() const { return valid; }
bool inherits_script(const Ref<Script> &p_script) const;
virtual Ref<Script> get_base_script() const; virtual Ref<Script> get_base_script() const;
virtual ScriptLanguage *get_language() const; virtual ScriptLanguage *get_language() const;

View file

@ -1265,6 +1265,10 @@ VisualScript::VisualScript() {
is_tool_script = false; is_tool_script = false;
} }
bool VisualScript::inherits_script(const Ref<Script> &p_script) const {
return this == p_script.ptr(); //there is no inheritance in visual scripts, so this is enough
}
StringName VisualScript::get_default_func() const { StringName VisualScript::get_default_func() const {
return StringName("f_312843592"); return StringName("f_312843592");
} }

View file

@ -260,6 +260,8 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
bool inherits_script(const Ref<Script> &p_script) const;
// TODO: Remove it in future when breaking changes are acceptable // TODO: Remove it in future when breaking changes are acceptable
StringName get_default_func() const; StringName get_default_func() const;
void add_function(const StringName &p_name); void add_function(const StringName &p_name);