Skip to content

Commit

Permalink
CraterCrashGH-811 Correctly initialize promoted variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Sep 22, 2024
1 parent a09d0bd commit d474132
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 138 deletions.
28 changes: 7 additions & 21 deletions src/editor/graph/graph_node_pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,50 +262,36 @@ void OrchestratorGraphNodePin::_remove_editable_pin()

void OrchestratorGraphNodePin::_promote_as_variable()
{
Orchestration* orchestation = _node->get_script_node()->get_orchestration();

Ref<OScriptVariable> variable = orchestation->create_variable(_create_promoted_variable_name(), _pin->get_type());
Ref<OScriptVariable> variable = _node->get_script_node()->get_orchestration()->promote_to_variable(_pin);
if (!variable.is_valid())
return;

variable->set_default_value(_pin->get_effective_default_value());

OScriptNodeInitContext context;
context.variable_name = variable->get_variable_name();

Vector2 offset = Vector2(200, 25);
Vector2 position = _node->get_script_node()->get_position();

if (is_input())
{
position -= offset;
position += get_graph_node()->get_input_port_position(_pin->get_pin_index());
position -= Vector2(250, 0);

get_graph()->spawn_node<OScriptNodeVariableGet>(context, position,
callable_mp_lambda(this, [&, this](const Ref<OScriptNodeVariableGet>& p_node) {
p_node->find_pin(0, PD_Output)->link(_pin);
}));
}
else
{
position += offset + Vector2(25, 0);
position += get_graph_node()->get_output_port_position(_pin->get_pin_index());
position += Vector2(75, 0);

get_graph()->spawn_node<OScriptNodeVariableSet>(context, position,
callable_mp_lambda(this, [&, this](const Ref<OScriptNodeVariableSet>& p_node) {
_pin->link(p_node->find_pin(1, PD_Input));
}));
}
}

String OrchestratorGraphNodePin::_create_promoted_variable_name()
{
Orchestration* orchestration = _node->get_script_node()->get_orchestration();

int index = 0;
String name = _pin->get_pin_name() + itos(index++);
while (orchestration->has_variable(name))
name = _pin->get_pin_name() + itos(index++);

return name;
}

void OrchestratorGraphNodePin::_create_widgets()
{
_default_value = nullptr;
Expand Down
4 changes: 0 additions & 4 deletions src/editor/graph/graph_node_pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,6 @@ class OrchestratorGraphNodePin : public HBoxContainer
/// Promote this pin to a variable
void _promote_as_variable();

/// Creates a new variable name
/// @return the variable name
String _create_promoted_variable_name();

/// Creates the pin's rendered icon
/// @param p_visible whether the icon is visible
/// @return the icon texture rect
Expand Down
25 changes: 25 additions & 0 deletions src/orchestration/orchestration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,31 @@ bool Orchestration::can_remove_variable(const StringName& p_name) const
return true;
}

Ref<OScriptVariable> Orchestration::promote_to_variable(const Ref<OScriptNodePin>& p_pin)
{
int index = 0;
String name = vformat("%s_%d", p_pin->get_pin_name(), index++);
while (has_variable(name))
name = vformat("%s_%d", p_pin->get_pin_name(), index++);

Ref<OScriptVariable> variable = create_variable(name);
if (variable.is_valid())
{
ClassificationParser parser;
if (parser.parse(p_pin->get_property_info()))
variable->set_classification(parser.get_classification());

variable->set_default_value(p_pin->get_effective_default_value());

variable->emit_changed();
variable->notify_property_list_changed();

_self->emit_signal("variables_changed");
}

return variable;
}

bool Orchestration::has_custom_signal(const StringName& p_name) const
{
return _signals.has(p_name);
Expand Down
1 change: 1 addition & 0 deletions src/orchestration/orchestration.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class Orchestration
Vector<Ref<OScriptVariable>> get_variables() const;
PackedStringArray get_variable_names() const;
bool can_remove_variable(const StringName& p_name) const;
Ref<OScriptVariable> promote_to_variable(const Ref<OScriptNodePin>& p_pin);
//~ End Variable Interface

//~ Begin Signals Interface
Expand Down
Loading

0 comments on commit d474132

Please sign in to comment.