2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* visual_script_yield_nodes.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 15:47:28 +01:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2016-08-08 00:22:33 +02:00
|
|
|
#include "visual_script_yield_nodes.h"
|
2017-03-05 15:47:28 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2016-08-08 00:22:33 +02:00
|
|
|
#include "scene/main/node.h"
|
2017-06-27 03:58:03 +02:00
|
|
|
#include "scene/main/scene_tree.h"
|
2016-08-08 00:22:33 +02:00
|
|
|
#include "visual_script_nodes.h"
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////YIELD///////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptYield::get_output_sequence_port_count() const {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool VisualScriptYield::has_input_sequence_port() const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int VisualScriptYield::get_input_value_port_count() const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int VisualScriptYield::get_output_value_port_count() const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptYield::get_output_sequence_port_text(int p_port) const {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PropertyInfo VisualScriptYield::get_input_value_port_info(int p_idx) const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return PropertyInfo();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PropertyInfo VisualScriptYield::get_output_value_port_info(int p_idx) const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return PropertyInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptYield::get_caption() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return yield_mode == YIELD_RETURN ? "Yield" : "Wait";
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptYield::get_text() const {
|
|
|
|
switch (yield_mode) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case YIELD_RETURN:
|
|
|
|
return "";
|
|
|
|
break;
|
|
|
|
case YIELD_FRAME:
|
|
|
|
return "Next Frame";
|
|
|
|
break;
|
|
|
|
case YIELD_PHYSICS_FRAME:
|
|
|
|
return "Next Physics Frame";
|
|
|
|
break;
|
|
|
|
case YIELD_WAIT:
|
|
|
|
return rtos(wait_time) + " sec(s)";
|
|
|
|
break;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
class VisualScriptNodeInstanceYield : public VisualScriptNodeInstance {
|
|
|
|
public:
|
|
|
|
VisualScriptYield::YieldMode mode;
|
|
|
|
float wait_time;
|
|
|
|
|
|
|
|
virtual int get_working_memory_size() const { return 1; } //yield needs at least 1
|
|
|
|
//virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
|
|
|
|
//virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_start_mode == START_MODE_RESUME_YIELD) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0; //resuming yield
|
|
|
|
} else {
|
|
|
|
//yield
|
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
SceneTree *tree = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
|
2016-08-08 00:22:33 +02:00
|
|
|
if (!tree) {
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error_str = "Main Loop is not SceneTree";
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<VisualScriptFunctionState> state;
|
2021-06-18 00:03:09 +02:00
|
|
|
state.instantiate();
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2016-08-25 22:45:20 +02:00
|
|
|
int ret = STEP_YIELD_BIT;
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (mode) {
|
|
|
|
case VisualScriptYield::YIELD_RETURN:
|
|
|
|
ret = STEP_EXIT_FUNCTION_BIT;
|
|
|
|
break; //return the yield
|
2020-05-10 13:00:47 +02:00
|
|
|
case VisualScriptYield::YIELD_FRAME:
|
|
|
|
state->connect_to_signal(tree, "idle_frame", Array());
|
|
|
|
break;
|
|
|
|
case VisualScriptYield::YIELD_PHYSICS_FRAME:
|
|
|
|
state->connect_to_signal(tree, "physics_frame", Array());
|
|
|
|
break;
|
|
|
|
case VisualScriptYield::YIELD_WAIT:
|
|
|
|
state->connect_to_signal(tree->create_timer(wait_time).ptr(), "timeout", Array());
|
|
|
|
break;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_working_mem = state;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2016-08-25 22:45:20 +02:00
|
|
|
return ret;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
VisualScriptNodeInstance *VisualScriptYield::instantiate(VisualScriptInstance *p_instance) {
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualScriptNodeInstanceYield *instance = memnew(VisualScriptNodeInstanceYield);
|
2016-08-08 00:22:33 +02:00
|
|
|
//instance->instance=p_instance;
|
2017-03-05 16:44:50 +01:00
|
|
|
instance->mode = yield_mode;
|
|
|
|
instance->wait_time = wait_time;
|
2016-08-08 00:22:33 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptYield::set_yield_mode(YieldMode p_mode) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (yield_mode == p_mode) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
yield_mode = p_mode;
|
2016-08-08 00:22:33 +02:00
|
|
|
ports_changed_notify();
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualScriptYield::YieldMode VisualScriptYield::get_yield_mode() {
|
2016-08-08 00:22:33 +02:00
|
|
|
return yield_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptYield::set_wait_time(float p_time) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (wait_time == p_time) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
wait_time = p_time;
|
2016-08-08 00:22:33 +02:00
|
|
|
ports_changed_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float VisualScriptYield::get_wait_time() {
|
2016-08-08 00:22:33 +02:00
|
|
|
return wait_time;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void VisualScriptYield::_validate_property(PropertyInfo &property) const {
|
|
|
|
if (property.name == "wait_time") {
|
|
|
|
if (yield_mode != YIELD_WAIT) {
|
|
|
|
property.usage = 0;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptYield::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_yield_mode", "mode"), &VisualScriptYield::set_yield_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_yield_mode"), &VisualScriptYield::get_yield_mode);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_wait_time", "sec"), &VisualScriptYield::set_wait_time);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_wait_time"), &VisualScriptYield::get_wait_time);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-10-21 16:28:08 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Frame,Physics Frame,Time", PROPERTY_USAGE_NOEDITOR), "set_yield_mode", "get_yield_mode");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wait_time"), "set_wait_time", "get_wait_time");
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(YIELD_FRAME);
|
2017-09-30 16:19:07 +02:00
|
|
|
BIND_ENUM_CONSTANT(YIELD_PHYSICS_FRAME);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(YIELD_WAIT);
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VisualScriptYield::VisualScriptYield() {
|
2017-03-05 16:44:50 +01:00
|
|
|
yield_mode = YIELD_FRAME;
|
|
|
|
wait_time = 1;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <VisualScriptYield::YieldMode MODE>
|
|
|
|
static Ref<VisualScriptNode> create_yield_node(const String &p_name) {
|
2016-08-08 00:22:33 +02:00
|
|
|
Ref<VisualScriptYield> node;
|
2021-06-18 00:03:09 +02:00
|
|
|
node.instantiate();
|
2016-08-08 00:22:33 +02:00
|
|
|
node->set_yield_mode(MODE);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////
|
|
|
|
////////////////YIELD SIGNAL//////////////////////
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptYieldSignal::get_output_sequence_port_count() const {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool VisualScriptYieldSignal::has_input_sequence_port() const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
Ref<Script> scr = p_current_node->get_script();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (scr.is_valid() && scr == script) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return p_current_node;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_current_node->get_child_count(); i++) {
|
|
|
|
Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (n) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return n;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
Node *VisualScriptYieldSignal::_get_base_node() const {
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
Ref<Script> script = get_visual_script();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!script.is_valid()) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MainLoop *main_loop = OS::get_singleton()->get_main_loop();
|
2017-08-24 22:58:51 +02:00
|
|
|
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!scene_tree) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
Node *edited_scene = scene_tree->get_edited_scene_root();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!edited_scene) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Node *script_node = _find_script_node(edited_scene, edited_scene, script);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!script_node) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!script_node->has_node(base_path)) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
Node *path_to = script_node->get_node(base_path);
|
|
|
|
|
|
|
|
return path_to;
|
|
|
|
#else
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2016-08-08 00:22:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
StringName VisualScriptYieldSignal::_get_base_type() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return get_visual_script()->get_instance_base_type();
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
|
2016-08-08 00:22:33 +02:00
|
|
|
Node *path = _get_base_node();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (path) {
|
2017-01-03 03:03:46 +01:00
|
|
|
return path->get_class();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return base_type;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int VisualScriptYieldSignal::get_input_value_port_count() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (call_mode == CALL_MODE_INSTANCE) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int VisualScriptYieldSignal::get_output_value_port_count() const {
|
2016-08-08 00:22:33 +02:00
|
|
|
MethodInfo sr;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!ClassDB::get_signal(_get_base_type(), signal, &sr)) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
return sr.arguments.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptYieldSignal::get_output_sequence_port_text(int p_port) const {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PropertyInfo VisualScriptYieldSignal::get_input_value_port_info(int p_idx) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (call_mode == CALL_MODE_INSTANCE) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return PropertyInfo(Variant::OBJECT, "instance");
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2016-08-08 00:22:33 +02:00
|
|
|
return PropertyInfo();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PropertyInfo VisualScriptYieldSignal::get_output_value_port_info(int p_idx) const {
|
2016-08-08 00:22:33 +02:00
|
|
|
MethodInfo sr;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!ClassDB::get_signal(_get_base_type(), signal, &sr)) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return PropertyInfo(); //no signal
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, sr.arguments.size(), PropertyInfo());
|
2016-08-08 00:22:33 +02:00
|
|
|
return sr.arguments[p_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptYieldSignal::get_caption() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
static const char *cname[3] = {
|
2016-08-08 00:22:33 +02:00
|
|
|
"WaitSignal",
|
|
|
|
"WaitNodeSignal",
|
|
|
|
"WaitInstanceSigna;",
|
|
|
|
};
|
|
|
|
|
|
|
|
return cname[call_mode];
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptYieldSignal::get_text() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (call_mode == CALL_MODE_SELF) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return " " + String(signal) + "()";
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
return " " + _get_base_type() + "." + String(signal) + "()";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void VisualScriptYieldSignal::set_base_type(const StringName &p_type) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (base_type == p_type) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
base_type = p_type;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2016-08-08 00:22:33 +02:00
|
|
|
ports_changed_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
StringName VisualScriptYieldSignal::get_base_type() const {
|
2016-08-08 00:22:33 +02:00
|
|
|
return base_type;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void VisualScriptYieldSignal::set_signal(const StringName &p_type) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (signal == p_type) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
signal = p_type;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2016-08-08 00:22:33 +02:00
|
|
|
ports_changed_notify();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-08-08 00:22:33 +02:00
|
|
|
StringName VisualScriptYieldSignal::get_signal() const {
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void VisualScriptYieldSignal::set_base_path(const NodePath &p_type) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (base_path == p_type) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
base_path = p_type;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2016-08-08 00:22:33 +02:00
|
|
|
ports_changed_notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
NodePath VisualScriptYieldSignal::get_base_path() const {
|
|
|
|
return base_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptYieldSignal::set_call_mode(CallMode p_mode) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (call_mode == p_mode) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
call_mode = p_mode;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2016-08-08 00:22:33 +02:00
|
|
|
ports_changed_notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
VisualScriptYieldSignal::CallMode VisualScriptYieldSignal::get_call_mode() const {
|
|
|
|
return call_mode;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void VisualScriptYieldSignal::_validate_property(PropertyInfo &property) const {
|
2017-07-01 02:30:17 +02:00
|
|
|
if (property.name == "base_type") {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (call_mode != CALL_MODE_INSTANCE) {
|
|
|
|
property.usage = PROPERTY_USAGE_NOEDITOR;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 02:30:17 +02:00
|
|
|
if (property.name == "node_path") {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (call_mode != CALL_MODE_NODE_PATH) {
|
|
|
|
property.usage = 0;
|
2016-08-08 00:22:33 +02:00
|
|
|
} else {
|
|
|
|
Node *bnode = _get_base_node();
|
|
|
|
if (bnode) {
|
2021-03-12 14:35:16 +01:00
|
|
|
property.hint_string = bnode->get_path(); //convert to long string
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 02:30:17 +02:00
|
|
|
if (property.name == "signal") {
|
2017-03-05 16:44:50 +01:00
|
|
|
property.hint = PROPERTY_HINT_ENUM;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
List<MethodInfo> methods;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::get_signal_list(_get_base_type(), &methods);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
List<String> mstring;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (E->get().name.begins_with("_")) {
|
2016-08-08 00:22:33 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
mstring.push_back(E->get().name.get_slice(":", 0));
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mstring.sort();
|
|
|
|
|
|
|
|
String ml;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<String>::Element *E = mstring.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (ml != String()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ml += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
ml += E->get();
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
property.hint_string = ml;
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptYieldSignal::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptYieldSignal::set_base_type);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptYieldSignal::get_base_type);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_signal", "signal"), &VisualScriptYieldSignal::set_signal);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptYieldSignal::get_signal);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptYieldSignal::set_call_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptYieldSignal::get_call_mode);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptYieldSignal::set_base_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptYieldSignal::get_base_path);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
String bt;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
bt += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bt += Variant::get_type_name(Variant::Type(i));
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 02:30:17 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "call_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance"), "set_call_mode", "get_call_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(CALL_MODE_SELF);
|
|
|
|
BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
|
|
|
|
BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class VisualScriptNodeInstanceYieldSignal : public VisualScriptNodeInstance {
|
|
|
|
public:
|
|
|
|
VisualScriptYieldSignal::CallMode call_mode;
|
|
|
|
NodePath node_path;
|
|
|
|
int output_args;
|
|
|
|
StringName signal;
|
|
|
|
|
|
|
|
VisualScriptYieldSignal *node;
|
|
|
|
VisualScriptInstance *instance;
|
|
|
|
|
|
|
|
virtual int get_working_memory_size() const { return 1; }
|
|
|
|
//virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
|
|
|
|
//virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_start_mode == START_MODE_RESUME_YIELD) {
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0; //resuming yield
|
|
|
|
} else {
|
|
|
|
//yield
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
Object *object = nullptr;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (call_mode) {
|
2016-08-08 00:22:33 +02:00
|
|
|
case VisualScriptYieldSignal::CALL_MODE_SELF: {
|
2017-03-05 16:44:50 +01:00
|
|
|
object = instance->get_owner_ptr();
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case VisualScriptYieldSignal::CALL_MODE_NODE_PATH: {
|
2017-08-24 22:58:51 +02:00
|
|
|
Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
|
2016-08-08 00:22:33 +02:00
|
|
|
if (!node) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error_str = "Base object is not a Node!";
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Node *another = node->get_node(node_path);
|
2017-08-25 17:14:33 +02:00
|
|
|
if (!another) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error_str = "Path does not lead Node!";
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
object = another;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case VisualScriptYieldSignal::CALL_MODE_INSTANCE: {
|
|
|
|
object = *p_inputs[0];
|
|
|
|
if (!object) {
|
2020-02-19 20:27:19 +01:00
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
2017-03-05 16:44:50 +01:00
|
|
|
r_error_str = "Supplied instance input is null.";
|
2016-08-08 00:22:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<VisualScriptFunctionState> state;
|
2021-06-18 00:03:09 +02:00
|
|
|
state.instantiate();
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
state->connect_to_signal(object, signal, Array());
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*p_working_mem = state;
|
2016-08-08 00:22:33 +02:00
|
|
|
|
|
|
|
return STEP_YIELD_BIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
VisualScriptNodeInstance *VisualScriptYieldSignal::instantiate(VisualScriptInstance *p_instance) {
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualScriptNodeInstanceYieldSignal *instance = memnew(VisualScriptNodeInstanceYieldSignal);
|
|
|
|
instance->node = this;
|
|
|
|
instance->instance = p_instance;
|
|
|
|
instance->signal = signal;
|
|
|
|
instance->call_mode = call_mode;
|
|
|
|
instance->node_path = base_path;
|
2016-08-08 00:22:33 +02:00
|
|
|
instance->output_args = get_output_value_port_count();
|
|
|
|
return instance;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-08-08 00:22:33 +02:00
|
|
|
VisualScriptYieldSignal::VisualScriptYieldSignal() {
|
2017-03-05 16:44:50 +01:00
|
|
|
call_mode = CALL_MODE_SELF;
|
|
|
|
base_type = "Object";
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <VisualScriptYieldSignal::CallMode cmode>
|
|
|
|
static Ref<VisualScriptNode> create_yield_signal_node(const String &p_name) {
|
2016-08-08 00:22:33 +02:00
|
|
|
Ref<VisualScriptYieldSignal> node;
|
2021-06-18 00:03:09 +02:00
|
|
|
node.instantiate();
|
2016-08-08 00:22:33 +02:00
|
|
|
node->set_call_mode(cmode);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_visual_script_yield_nodes() {
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_frame", create_yield_node<VisualScriptYield::YIELD_FRAME>);
|
2017-09-30 16:19:07 +02:00
|
|
|
VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_physics_frame", create_yield_node<VisualScriptYield::YIELD_PHYSICS_FRAME>);
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_time", create_yield_node<VisualScriptYield::YIELD_WAIT>);
|
2016-08-08 00:22:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VisualScriptLanguage::singleton->add_register_func("functions/yield", create_yield_node<VisualScriptYield::YIELD_RETURN>);
|
|
|
|
VisualScriptLanguage::singleton->add_register_func("functions/yield_signal", create_node_generic<VisualScriptYieldSignal>);
|
2016-08-08 00:22:33 +02:00
|
|
|
}
|