Skip to content

Commit

Permalink
GH-551 Avoid duplicating return nodes on rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Jul 21, 2024
1 parent d50b28c commit 1768f9a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 46 deletions.
43 changes: 0 additions & 43 deletions src/editor/graph/graph_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,6 @@ void OrchestratorGraphEdit::_notification(int p_what)
_script_graph->connect("node_removed", callable_mp(this, &OrchestratorGraphEdit::_on_graph_node_removed));
_script_graph->connect("knots_updated", callable_mp(this, &OrchestratorGraphEdit::_synchronize_graph_knots));

if (_script_graph->get_flags().has_flag(OScriptGraph::GraphFlags::GF_FUNCTION))
{
Ref<OScriptFunction> function = _script_graph->get_functions()[0];
if (function.is_valid())
function->connect("changed", callable_mp(this, &OrchestratorGraphEdit::_function_changed));
}

// Wire up action menu
_action_menu->connect("canceled", callable_mp(this, &OrchestratorGraphEdit::_on_action_menu_cancelled));
_action_menu->connect("action_selected", callable_mp(this, &OrchestratorGraphEdit::_on_action_menu_action_selected));
Expand Down Expand Up @@ -1197,42 +1190,6 @@ void OrchestratorGraphEdit::_hide_drag_hint()
_drag_hint->hide();
}

void OrchestratorGraphEdit::_function_changed()
{
Vector<Ref<OScriptFunction>> functions = _script_graph->get_functions();
if (functions.is_empty())
return;

const Ref<OScriptFunction> function = functions[0];
if (function.is_valid() && function->has_return_type())
{
Ref<OScriptNodeFunctionResult> result = function->get_return_node();
if (!result.is_valid())
{
Ref<OScriptNodeFunctionEntry> entry = function->get_owning_node();

// Check whether we should autowire the return to the entry
bool autowire = false;
Ref<OScriptNodePin> entry_exec_out = entry->get_execution_pin();
if (entry_exec_out.is_valid() && !entry_exec_out->has_any_connections())
autowire = true;

// The spawn location
const Vector2 position = entry->get_position() + Vector2(400, 0);

// Create context
OScriptNodeInitContext context;
context.method = function->get_method_info();

spawn_node<OScriptNodeFunctionResult>(context, position,
callable_mp_lambda(this, [&] (const Ref<OScriptNodeFunctionResult>& node) {
if (autowire)
_script_graph->link(entry->get_id(), 0, node->get_id(), 0);
}));
}
}
}

void OrchestratorGraphEdit::_on_connection_drag_started(const StringName& p_from, int p_from_port, bool p_output)
{
OrchestratorSettings* os = OrchestratorSettings::get_singleton();
Expand Down
3 changes: 0 additions & 3 deletions src/editor/graph/graph_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ class OrchestratorGraphEdit : public GraphEdit
/// Hides the drag status hint
void _hide_drag_hint();

/// Dispatched when the script's function is changed
void _function_changed();

/// Connection drag started
/// @param p_from the source node
/// @param p_from_port source node port
Expand Down
22 changes: 22 additions & 0 deletions src/script/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "common/dictionary_utils.h"
#include "common/method_utils.h"
#include "common/property_utils.h"
#include "nodes/functions/function_entry.h"
#include "nodes/functions/function_result.h"
#include "script/script.h"

Expand Down Expand Up @@ -295,6 +296,27 @@ void OScriptFunction::set_return(const PropertyInfo& p_property)
_method.return_val = p_property;
_returns_value = MethodUtils::has_return_value(_method);

if (_returns_value)
{
// Since function returns a value, if there is no result node, add one.
// If the function entry exec pin is not yet wired, autowire it to the result node.
Ref<OScriptNodeFunctionResult> result = get_return_node();
if (!result.is_valid())
{
const Ref<OScriptNodeFunctionEntry> entry = get_owning_node();
const Vector2 position = entry->get_position() + Vector2(400, 0);

OScriptNodeInitContext context;
context.method = get_method_info();

result = get_function_graph()->create_node<OScriptNodeFunctionResult>(context, position);

const Ref<OScriptNodePin> entry_exec_out = entry->get_execution_pin();
if (entry_exec_out.is_valid() && !entry_exec_out->has_any_connections() && result.is_valid())
get_function_graph()->link(entry->get_id(), 0, result->get_id(), 0);
}
}

emit_changed();
notify_property_list_changed();
}
Expand Down

0 comments on commit 1768f9a

Please sign in to comment.