Using wildcards - HiIAmMoot/RuntimeBPs GitHub Wiki

Using wildcards

Wildcards can be used for functions that only modify arrays. We are nog actually accessing the values in the array so we do not need to know the type to execute these kinds of functions. An example for this is a ForEach.

UForEachLoop::UForEachLoop()
{
	NodeName = "For Each Loop";

	InputPins.SetNum(2);
	InputPins[0].MakeNodePin();// No args means execute
	InputPins[1].MakeNodeArray("Array", EVariableTypes::WildCard);

	OutputPins.SetNum(4);
	OutputPins[0].MakeNodePin("Loop Body");
	OutputPins[1].MakeNodePin("Array Element", EVariableTypes::WildCard);
	OutputPins[2].MakeNodePin("Array Index", EVariableTypes::Int);
	OutputPins[3].MakeNodePin("Completed");
}

void UForEachLoop::Execute(int Index, int FromLoopIndex)
{
	CurrentLoopIndex = 0;
	// Copy the array so we don't have to go down the chain to get the array for each iteration
	Array = GetConnectedPinArray(InputPins[1]).Array;
	LastIndex = Array.Num() - 1;
	ReceivedFromLoopIndex = FromLoopIndex;
	if (LastIndex >= 0 && CurrentLoopIndex >= 0)
	{
		Next();
	}
	else
	{
		Super::Execute(3, FromLoopIndex);// On Completed
	}
}

// This function is automatically called again by the last function in the execution chain by checking FromLoopIndex
void UForEachLoop::Next()
{
	if (CurrentLoopIndex <= LastIndex)
	{
		// Array element, we don't need to know what kind of array it is to pass the value
		OutputPins[1].Value.Array[0] = Array[CurrentLoopIndex];
		// Array index
		OutputPins[2].Value.Array[0].SetIntArg(CurrentLoopIndex);

		// Doing a sleep every 1024 iterations to not overload the thread
		if (BPConstructor->GetMultiThread() && !(CurrentLoopIndex & 1023))
		{
			FPlatformProcess::Sleep(0.01);
			CurrentLoopIndex++;
			Super::Execute(0, NodeIndex);// Loop Body
		}
		else
		{
			CurrentLoopIndex++;
			Super::Execute(0, NodeIndex);// Loop Body
		}
	}
	else
	{
		FPlatformProcess::Sleep(0.01);
		Super::Execute(3, ReceivedFromLoopIndex);// On Completed
	}
}