Path: blob/master/scene/main/instance_placeholder.cpp
9896 views
/**************************************************************************/1/* instance_placeholder.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "instance_placeholder.h"3132#include "core/io/resource_loader.h"33#include "scene/resources/packed_scene.h"3435bool InstancePlaceholder::_set(const StringName &p_name, const Variant &p_value) {36PropSet ps;37ps.name = p_name;38ps.value = p_value;39stored_values.push_back(ps);40return true;41}4243bool InstancePlaceholder::_get(const StringName &p_name, Variant &r_ret) const {44for (const PropSet &E : stored_values) {45if (E.name == p_name) {46r_ret = E.value;47return true;48}49}50return false;51}5253void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {54for (const PropSet &E : stored_values) {55PropertyInfo pi;56pi.name = E.name;57pi.type = E.value.get_type();58pi.usage = PROPERTY_USAGE_STORAGE;5960p_list->push_back(pi);61}62}6364void InstancePlaceholder::set_instance_path(const String &p_name) {65path = p_name;66}6768String InstancePlaceholder::get_instance_path() const {69return path;70}7172Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene> &p_custom_scene) {73ERR_FAIL_COND_V(!is_inside_tree(), nullptr);7475Node *base = get_parent();76if (!base) {77return nullptr;78}7980Ref<PackedScene> ps;81if (p_custom_scene.is_valid()) {82ps = p_custom_scene;83} else {84ps = ResourceLoader::load(path, "PackedScene");85}8687if (ps.is_null()) {88return nullptr;89}90Node *instance = ps->instantiate();91if (!instance) {92return nullptr;93}94instance->set_name(get_name());95instance->set_multiplayer_authority(get_multiplayer_authority());96int pos = get_index();9798for (const PropSet &E : stored_values) {99set_value_on_instance(this, instance, E);100}101102if (p_replace) {103queue_free();104base->remove_child(this);105}106107base->add_child(instance);108base->move_child(instance, pos);109110return instance;111}112113// This method will attempt to set the correct values on the placeholder instance114// for regular types this is trivial and unnecessary.115// For nodes however this becomes a bit tricky because they might now have existed until the instantiation,116// so this method will try to find the correct nodes and resolve them.117void InstancePlaceholder::set_value_on_instance(InstancePlaceholder *p_placeholder, Node *p_instance, const PropSet &p_set) {118bool is_valid;119120// If we don't have any info, we can't do anything,121// so try setting the value directly.122Variant current = p_instance->get(p_set.name, &is_valid);123if (!is_valid) {124p_instance->set(p_set.name, p_set.value, &is_valid);125return;126}127128Variant::Type current_type = current.get_type();129Variant::Type placeholder_type = p_set.value.get_type();130131// Arrays are a special case, because their containing type might be different.132if (current_type != Variant::Type::ARRAY) {133// Check if the variant types match.134if (Variant::evaluate(Variant::OP_EQUAL, current_type, placeholder_type)) {135p_instance->set(p_set.name, p_set.value, &is_valid);136if (is_valid) {137return;138}139// Types match but setting failed? This is strange, so let's print a warning!140WARN_PRINT(vformat("Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(current_type), p_placeholder->get_name()));141return;142}143} else {144// We are dealing with an Array.145// Let's check if the subtype of the array matches first.146// This is needed because the set method of ScriptInstance checks for type,147// but the ClassDB set method doesn't! So we cannot reliably know what actually happens.148Array current_array = current;149Array placeholder_array = p_set.value;150if (current_array.is_same_typed(placeholder_array)) {151p_instance->set(p_set.name, p_set.value, &is_valid);152if (is_valid) {153return;154}155// Internal array types match but setting failed? This is strange, so let's print a warning!156WARN_PRINT(vformat("Array Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(Variant::Type(current_array.get_typed_builtin())), p_placeholder->get_name()));157}158// Arrays are not the same internal type. This should be happening because we have a NodePath Array,159// but the instance wants a Node Array.160}161162switch (current_type) {163case Variant::Type::NIL: {164Ref<Resource> resource = p_set.value;165if (placeholder_type != Variant::Type::NODE_PATH && resource.is_null()) {166break;167}168// If it's nil but we have a NodePath or a Resource, we guess what works.169p_instance->set(p_set.name, p_set.value, &is_valid);170if (is_valid) {171break;172}173174p_instance->set(p_set.name, try_get_node(p_placeholder, p_instance, p_set.value), &is_valid);175break;176}177case Variant::Type::OBJECT: {178if (placeholder_type != Variant::Type::NODE_PATH) {179break;180}181// Easiest case, we want a node, but we have a deferred NodePath.182p_instance->set(p_set.name, try_get_node(p_placeholder, p_instance, p_set.value));183break;184}185case Variant::Type::ARRAY: {186// If we have reached here it means our array types don't match,187// so we will convert the placeholder array into the correct type188// and resolve nodes if necessary.189Array current_array = current;190Array converted_array;191Array placeholder_array = p_set.value;192converted_array = current_array.duplicate();193converted_array.resize(placeholder_array.size());194195if (Variant::evaluate(Variant::OP_EQUAL, current_array.get_typed_builtin(), Variant::Type::NODE_PATH)) {196// We want a typed NodePath array.197for (int i = 0; i < placeholder_array.size(); i++) {198converted_array.set(i, placeholder_array[i]);199}200} else {201// We want Nodes, convert NodePaths.202for (int i = 0; i < placeholder_array.size(); i++) {203converted_array.set(i, try_get_node(p_placeholder, p_instance, placeholder_array[i]));204}205}206207p_instance->set(p_set.name, converted_array, &is_valid);208if (!is_valid) {209WARN_PRINT(vformat("Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(current_type), p_placeholder->get_name()));210}211break;212}213default: {214WARN_PRINT(vformat("Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(current_type), p_placeholder->get_name()));215break;216}217}218}219220Node *InstancePlaceholder::try_get_node(InstancePlaceholder *p_placeholder, Node *p_instance, const NodePath &p_path) {221// First try to resolve internally,222// if that fails try resolving externally.223Node *node = p_instance->get_node_or_null(p_path);224if (node == nullptr) {225node = p_placeholder->get_node_or_null(p_path);226}227228return node;229}230231Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {232Dictionary ret;233PackedStringArray order;234235for (const PropSet &E : stored_values) {236ret[E.name] = E.value;237if (p_with_order) {238order.push_back(E.name);239}240};241242if (p_with_order) {243ret[".order"] = order;244}245246return ret;247}248249void InstancePlaceholder::_bind_methods() {250ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));251ClassDB::bind_method(D_METHOD("create_instance", "replace", "custom_scene"), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant()));252ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);253}254255InstancePlaceholder::InstancePlaceholder() {256}257258259