/*************************************************************************/ /* 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