Browse Source

Implement inherits_script() for NativeScript and PluginScript

(cherry picked from commit 2dcd064056cd7905ef1559e9f35aa223a583d225)
Pedro J. Estébanez 4 years ago
parent
commit
d272464e6e

+ 19 - 1
modules/gdnative/nativescript/nativescript.cpp

@@ -112,7 +112,25 @@ void NativeScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder)
 #endif
 #endif
 
 
 bool NativeScript::inherits_script(const Ref<Script> &p_script) const {
 bool NativeScript::inherits_script(const Ref<Script> &p_script) const {
-	WARN_PRINT_ONCE("Inheritance needs to be implemented in NativeScript.");
+	Ref<NativeScript> ns = p_script;
+	if (ns.is_null()) {
+		return false;
+	}
+
+	const NativeScriptDesc *other_s = ns->get_script_desc();
+	if (!other_s) {
+		return false;
+	}
+
+	const NativeScriptDesc *s = get_script_desc();
+
+	while (s) {
+		if (s == other_s) {
+			return true;
+		}
+		s = s->base_data;
+	}
+
 	return false;
 	return false;
 }
 }
 
 

+ 14 - 1
modules/gdnative/pluginscript/pluginscript_script.cpp

@@ -137,7 +137,20 @@ bool PluginScript::can_instance() const {
 }
 }
 
 
 bool PluginScript::inherits_script(const Ref<Script> &p_script) const {
 bool PluginScript::inherits_script(const Ref<Script> &p_script) const {
-	WARN_PRINT_ONCE("Inheritance needs to be implemented in PluginScript.");
+	Ref<PluginScript> ps = p_script;
+	if (ps.is_null()) {
+		return false;
+	}
+
+	const PluginScript *s = this;
+
+	while (s) {
+		if (s == p_script.ptr()) {
+			return true;
+		}
+		s = Object::cast_to<PluginScript>(s->_ref_base_parent.ptr());
+	}
+
 	return false;
 	return false;
 }
 }