Trying to do a K2Node implementation in C++ but something is wrong about Intermediate Connections

Hi I’m, first of all I’m fairly new with Unreal C++, I have other backgroudn like Kotlin, Java, Javascript, Python or Apex(My real job.) but on this I’m basically new. Second, my main language isn’t Spanish, so sorry if I make a lot of error in spelling :flushed:

So, the use case is the next: I want to do a If-ElseIf K2Node, I don’t like the Blueprint Branching Node and I want to make an implementation that allows me to do the IF-ElseIf behaviour. I’m close to accomplish that. This is what I have.

This how the blueprint looks within the Blueprint Graph.

And this is the implementation of the expand note…

void UIfElse::ExpandNode(class FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
        //Initializing all variables that are gonna be used along the entire instation.
	Super::ExpandNode(CompilerContext, SourceGraph);
	CompilerContext.MessageLog.Note(TEXT("ExpandNode iniciado"));
	TArray<UEdGraphPin*> InputPins = TArray<UEdGraphPin*>();
	TArray<UEdGraphPin*> OutputPins = TArray<UEdGraphPin*>();
	UEdGraphPin* InputExec = nullptr;
	TArray<UK2Node_IfThenElse*> IfThenElseList = TArray<UK2Node_IfThenElse*>();
	
        //Identifies the pins and make a lists for each type of Pin.
	for (UEdGraphPin* Pin : Pins)
	{
		if (Pin->PinType.PinCategory == UEdGraphSchema_K2::PC_Exec && Pin->Direction == EGPD_Input)
		{
			InputExec = Pin;
			CompilerContext.MessageLog.Note(TEXT("Input Exec Found"));
		}else if (Pin->PinType.PinCategory == UEdGraphSchema_K2::PC_Boolean)
		{
			InputPins.Add(Pin);
			CompilerContext.MessageLog.Note(TEXT("Boolean Pin Found"));
		}else if (Pin->PinType.PinCategory == UEdGraphSchema_K2::PC_Exec && Pin->Direction == EGPD_Output)
		{
			OutputPins.Add(Pin);
			CompilerContext.MessageLog.Note(TEXT("Output Exec Pin Found: "));
		}
	}
	

        //If no pins are created throws a warning to the blueprint graph
	if (InputPins.Num() == 0 || OutputPins.Num() == 0)
	{
		CompilerContext.MessageLog.Warning(TEXT("There are no inputs in the lists"));
	}
	
        //This is the important thing, makes a Branching tree so When the Condition is true 
        //Reproduce then and if is not, connects else to the next Branch until there's no more conditions.
	for (int32 PinIndex = 0; PinIndex < InputPins.Num(); PinIndex++)
	{
		UEdGraphPin* ConditionPin = InputPins[PinIndex];
		UEdGraphPin* OutputPin = OutputPins[PinIndex];

		
		if (ConditionPin && OutputPin)
		{
			UK2Node_IfThenElse* BranchNode = nullptr;			
			BranchNode =  CompilerContext.SpawnIntermediateNode<UK2Node_IfThenElse>(this, SourceGraph);
			
			BranchNode -> AllocateDefaultPins();
			CompilerContext.MovePinLinksToIntermediate(*ConditionPin, *BranchNode->GetConditionPin());
			CompilerContext.MovePinLinksToIntermediate(*OutputPin, *BranchNode->GetThenPin());
			if (!BranchNode)
			{
				CompilerContext.MessageLog.Warning(TEXT("There is no BranchNode or could not be created"));
			}
			if (IfThenElseList.Num() == 0)
			{
				CompilerContext.MovePinLinksToIntermediate(*InputExec, *BranchNode->GetExecPin());
				CompilerContext.MessageLog.Note(TEXT("Added RootNode"));
				IfThenElseList.Add(BranchNode);
			}else
			{
				UEdGraphPin* ElsePin = IfThenElseList.Last()->GetElsePin();
				UEdGraphPin* ExecPin = BranchNode->GetExecPin();
				
				CompilerContext.MovePinLinksToIntermediate(*ElsePin, *ExecPin);
				IfThenElseList.Add(BranchNode);
				CompilerContext.MessageLog.Note(TEXT("Added Else Node"));
			}
		}
	}

	BreakAllNodeLinks();
}

The only problem with this implementation is the next, is seems like the else are connected in a wrong way, because does not reproduce any logic from the else, so taking the node image that I share previousle IF(the default pin boolean) is false, checks if the next is true, and if is false checks the next an so on until theres no more conditions to check, then and finally, stop the execution.

But the only pin it works is the default pin that connects with the first then, none other works, the pins are detected in the correct order and connected, in Rider I debugged all that, the only thing I do not how to debug is the hidden intermediate flow of the K2Node.

Thanks in advance for your help!

i think this is part of the problem, you’re connecting each branch to the InputExec but that would break previous connections so only the last IfNode would be connected.

usually you’d cache a UEdGraphPin* LastThen = InputExec outside the loop and then in the loop update LastThen = BranchElseNode