The error you're encountering:
COMPILER ERROR: failed building connection with 'Directions are not compatible' at CustomNode
occurs because you're trying to move pin links between pins that have incompatible directions (specifically, from an input pin to an output pin). In your ExpandNode function, you're attempting to move links from an input execution pin (ExecPin) to an output execution pin (ThenPin):
CompilerContext.MovePinLinksToIntermediate(*ExecPin, *ThenPin);
This operation isn't allowed because the pin directions don't match—ExecPin is an input pin, and ThenPin is an output pin. The MovePinLinksToIntermediate function expects the pins to have the same direction.
To fix this issue, you need to rewire the execution flow correctly by connecting the nodes linked to your ExecPin directly to the nodes linked to your ThenPin. Here's how you can modify your ExpandNode function:
void UCustomK2Node::ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
Super::ExpandNode(CompilerContext, SourceGraph);
// Get the execution pins
UEdGraphPin* ExecPin = GetExecPin();
UEdGraphPin* ThenPin = FindPinChecked(UEdGraphSchema_K2::PN_Then);
// Collect arrays of pins connected to ExecPin and ThenPin
TArray<UEdGraphPin*> ExecPinLinks = ExecPin->LinkedTo;
TArray<UEdGraphPin*> ThenPinLinks = ThenPin->LinkedTo;
// Rewire the execution flow: connect nodes linked to ExecPin to nodes linked to ThenPin
for (UEdGraphPin* InputPin : ExecPinLinks)
{
for (UEdGraphPin* OutputPin : ThenPinLinks)
{
CompilerContext.GetSchema()->TryCreateConnection(InputPin, OutputPin);
}
}
// Break the links to the original node to remove it from the graph
BreakAllNodeLinks();
}
Explanation:
-
Collect Links: We gather all the pins connected to ExecPin and ThenPin.
-
Rewire Execution Flow: We loop over each pair of connected pins to ExecPin (incoming execution flows) and ThenPin (outgoing execution flows) and create connections between them. This effectively bypasses your custom node in the execution flow, connecting the preceding nodes directly to the subsequent nodes.
-
Break Links: Finally, we call BreakAllNodeLinks() to remove your custom node from the graph since it's been effectively replaced.
Why This Works:
By rewiring the execution pins in this manner, we ensure that:
-
Pin Directions Match: We're connecting output pins (from nodes connected to ExecPin) to input pins (of nodes connected to ThenPin), which aligns with the expected flow in execution graphs.
-
Maintaining Execution Flow: The execution flow remains intact, and there are no breaks or invalid connections in the graph.
Additional Notes:
-
Avoid Moving Between Different Directions: Always ensure when moving or connecting pins that the pin directions are compatible (input to input or output to output).
-
Super Calls: Keep your Super::ExpandNode call at the beginning unless there's a specific reason to override base functionality after your custom logic.
-
Alternative Approaches: If your node is intended to perform some action during execution, you might need to create intermediate nodes (like UK2Node_CallFunction) and wire them appropriately. This example assumes that your node doesn't need to perform any action and effectively removes it from the execution flow.
Example Modification:
Here's a more detailed version with comments:
void UCustomK2Node::ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
Super::ExpandNode(CompilerContext, SourceGraph);
// Get the execution pins of your node
UEdGraphPin* ExecPin = GetExecPin();
UEdGraphPin* ThenPin = FindPinChecked(UEdGraphSchema_K2::PN_Then);
// Check if pins are valid
if (!ExecPin || !ThenPin)
{
CompilerContext.MessageLog.Error(*FString::Printf(TEXT("CustomNode has invalid execution pins.")));
BreakAllNodeLinks();
return;
}
// Copy the pins to avoid modification during iteration
TArray<UEdGraphPin*> ExecPinLinks = ExecPin->LinkedTo;
TArray<UEdGraphPin*> ThenPinLinks = ThenPin->LinkedTo;
// Rewire the execution flow: connect each node linked to ExecPin to each node linked to ThenPin
for (UEdGraphPin* IncomingPin : ExecPinLinks)
{
for (UEdGraphPin* OutgoingPin : ThenPinLinks)
{
// Create a direct connection
CompilerContext.GetSchema()->TryCreateConnection(IncomingPin, OutgoingPin);
}
}
// Disconnect the node from the graph
BreakAllNodeLinks();
}
Summary:
You're getting the error because you're attempting to move pin links between pins with incompatible directions. By rewiring the execution flow as shown, you can avoid this issue and ensure your custom node integrates correctly into the execution graph.