Skip to content

Commit

Permalink
GH-756 Fix validated variable getter state consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Aug 25, 2024
1 parent 9682629 commit 4e3e99f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/script/nodes/variables/variable_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,29 @@ void OScriptNodeVariableGet::_variable_changed()
{
if (_is_in_editor())
{
Ref<OScriptNodePin> output = find_pin("value", PD_Output);
if (output.is_valid() && output->has_any_connections())
if (!can_be_validated() && _validated)
set_validated(false);

// Defer this so that all nodes have updated
// This is necessary so that all target types that may have changed (i.e. get connected to set)
// have updated to make sure that the "can_accept" logic works as expected.
callable_mp(this, &OScriptNodeVariableGet::_validate_output_connection).call_deferred();
}

super::_variable_changed();
}

void OScriptNodeVariableGet::_validate_output_connection()
{
Ref<OScriptNodePin> output = find_pin("value", PD_Output);
if (output.is_valid() && output->has_any_connections())
{
for (const Ref<OScriptNodePin>& target : output->get_connections())
{
Ref<OScriptNodePin> target = output->get_connections()[0];
if (target.is_valid() && !target->can_accept(output))
output->unlink_all();
output->unlink(target);
}
}

super::_variable_changed();
}

void OScriptNodeVariableGet::allocate_default_pins()
Expand Down Expand Up @@ -196,22 +209,27 @@ void OScriptNodeVariableGet::set_validated(bool p_validated)
}

// Record the connection before the change
Ref<OScriptNodePin> connection;
Vector<Ref<OScriptNodePin>> connections;
Ref<OScriptNodePin> value = find_pin("value", PD_Output);
if (value.is_valid() && value->has_any_connections())
{
connection = value->get_connections()[0];
for (const Ref<OScriptNodePin>& target : value->get_connections())
connections.push_back(target);

value->unlink_all();
}

_notify_pins_changed();

if (connection.is_valid())
if (!connections.is_empty())
{
// Relink connection on change
value = find_pin("value", PD_Output);
if (value.is_valid())
value->link(connection);
{
for (const Ref<OScriptNodePin>& target : connections)
value->link(target);
}
}
}
}
2 changes: 2 additions & 0 deletions src/script/nodes/variables/variable_get.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class OScriptNodeVariableGet : public OScriptNodeVariable
void _variable_changed() override;
//~ End OScriptNodeVariable Interface

void _validate_output_connection();

public:
//~ Begin OScriptNode Interface
void allocate_default_pins() override;
Expand Down

0 comments on commit 4e3e99f

Please sign in to comment.