From 0de61998e42b368efe90ad07838aea99752447c0 Mon Sep 17 00:00:00 2001 From: Emmanuel Leblond Date: Mon, 15 Jan 2018 23:20:02 +0100 Subject: Improve inheritance system in gdnative pluginscript --- .../gdnative/pluginscript/pluginscript_script.cpp | 49 +++++++++++----------- 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'modules/gdnative/pluginscript/pluginscript_script.cpp') diff --git a/modules/gdnative/pluginscript/pluginscript_script.cpp b/modules/gdnative/pluginscript/pluginscript_script.cpp index b4525ff8aa..8290582b9b 100644 --- a/modules/gdnative/pluginscript/pluginscript_script.cpp +++ b/modules/gdnative/pluginscript/pluginscript_script.cpp @@ -131,13 +131,10 @@ ScriptInstance *PluginScript::instance_create(Object *p_this) { #endif } - PluginScript *top = this; - // TODO: can be optimized by storing a PluginScript::_base_parent direct pointer - while (top->_ref_base_parent.is_valid()) - top = top->_ref_base_parent.ptr(); - if (top->_native_parent) { - if (!ClassDB::is_parent_class(p_this->get_class_name(), top->_native_parent)) { - String msg = "Script inherits from native type '" + String(top->_native_parent) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'"; + StringName base_type = get_instance_base_type(); + if (base_type) { + if (!ClassDB::is_parent_class(p_this->get_class_name(), base_type)) { + String msg = "Script inherits from native type '" + String(base_type) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'"; // TODO: implement PluginscriptLanguage::debug_break_parse // if (ScriptDebugger::get_singleton()) { // _language->debug_break_parse(get_path(), 0, msg); @@ -210,29 +207,31 @@ Error PluginScript::reload(bool p_keep_state) { // TODO: GDscript uses `ScriptDebugger` here to jump into the parsing error return err; } + + // Script's parent is passed as base_name which can make reference to a + // ClassDB name (i.e. `Node2D`) or a ressource path (i.e. `res://foo/bar.gd`) + StringName *base_name = (StringName *)&manifest.base; + if (*base_name) { + + if (ClassDB::class_exists(*base_name)) { + _native_parent = *base_name; + } else { + Ref