From 885239fb09da503472bb4def4754036e70b43f1a Mon Sep 17 00:00:00 2001 From: Karroffel Date: Mon, 24 Apr 2017 00:29:17 +0200 Subject: re-added MultiScript The very first Godot version (when it was open sourced) had "MultiScript" which lets you use multiple scripts on one object. With the addition of mulitple new scripting languages (VisualScript, soon C# and GDNative) it can be of use to combine scripts rather than delegating (with huge maintainance cost) or creating child nodes which could impact performance. I used the code from 0b806ee as the base and made it work with the current master. --- modules/multiscript/multiscript.cpp | 750 ++++++++++++++++++++++++++++++++++++ 1 file changed, 750 insertions(+) create mode 100644 modules/multiscript/multiscript.cpp (limited to 'modules/multiscript/multiscript.cpp') diff --git a/modules/multiscript/multiscript.cpp b/modules/multiscript/multiscript.cpp new file mode 100644 index 0000000000..b2633b7207 --- /dev/null +++ b/modules/multiscript/multiscript.cpp @@ -0,0 +1,750 @@ +/*************************************************************************/ +/* multiscript.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "multiscript.h" + +bool MultiScriptInstance::set(const StringName &p_name, const Variant &p_value) { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + bool found = sarr[i]->set(p_name, p_value); + if (found) + return true; + } + + if (String(p_name).begins_with("script_")) { + bool valid; + owner->set(p_name, p_value, &valid); + return valid; + } + return false; +} + +bool MultiScriptInstance::get(const StringName &p_name, Variant &r_ret) const { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + bool found = sarr[i]->get(p_name, r_ret); + if (found) + return true; + } + if (String(p_name).begins_with("script_")) { + bool valid; + r_ret = owner->get(p_name, &valid); + return valid; + } + return false; +} +void MultiScriptInstance::get_property_list(List *p_properties) const { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + Set existing; + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + List pl; + sarr[i]->get_property_list(&pl); + + for (List::Element *E = pl.front(); E; E = E->next()) { + + if (existing.has(E->get().name)) + continue; + + p_properties->push_back(E->get()); + existing.insert(E->get().name); + } + } + + p_properties->push_back(PropertyInfo(Variant::NIL, "Scripts", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); + + for (int i = 0; i < owner->scripts.size(); i++) { + + p_properties->push_back(PropertyInfo(Variant::OBJECT, "script_" + String::chr('a' + i), PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_EDITOR)); + } + + if (owner->scripts.size() < 25) { + + p_properties->push_back(PropertyInfo(Variant::OBJECT, "script_" + String::chr('a' + (owner->scripts.size())), PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_EDITOR)); + } +} + +void MultiScriptInstance::get_method_list(List *p_list) const { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + Set existing; + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + List ml; + sarr[i]->get_method_list(&ml); + + for (List::Element *E = ml.front(); E; E = E->next()) { + + if (existing.has(E->get().name)) + continue; + + p_list->push_back(E->get()); + existing.insert(E->get().name); + } + } +} +bool MultiScriptInstance::has_method(const StringName &p_method) const { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + if (sarr[i]->has_method(p_method)) + return true; + } + + return false; +} + +Variant MultiScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + Variant r = sarr[i]->call(p_method, p_args, p_argcount, r_error); + if (r_error.error == Variant::CallError::CALL_OK) + return r; + else if (r_error.error != Variant::CallError::CALL_ERROR_INVALID_METHOD) + return r; + } + + r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); +} + +void MultiScriptInstance::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) { + + ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + for (int i = 0; i < sc; i++) { + + if (!sarr[i]) + continue; + + sarr[i]->call_multilevel(p_method, p_args, p_argcount); + } +} +void MultiScriptInstance::notification(int p_notification) { + + // ScriptInstance **sarr = instances.ptr(); + int sc = instances.size(); + + for (int i = 0; i < sc; i++) { + + ScriptInstance *instance = instances[i]; + + if (!instance) + continue; + + instance->notification(p_notification); + } +} + +Ref