Untitled

 avatar
user_8265478
plain_text
8 months ago
4.3 kB
1
Indexable
Never
#include "Nodes/Route/FlowNode_ExecutionMultiGate.h"

UFlowNode_ExecutionMultiGate::UFlowNode_ExecutionMultiGate(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	, StartIndex(INDEX_NONE)
{
#if WITH_EDITOR
	Category = TEXT("Route");
	NodeStyle = EFlowNodeStyle::Logic;
#endif

	FString ResetPinTooltip = TEXT("Finish work of this node.");
	ResetPinTooltip += LINE_TERMINATOR;
	ResetPinTooltip += TEXT("Calling In input will start triggering output pins once again.");

	InputPins.Add(FFlowPin(TEXT("Reset"), ResetPinTooltip));
	SetNumberedOutputPins(0, 1);
	OutputPins.Add(FFlowPin(TEXT("Completed")));
	AllowedSignalModes = {EFlowSignalMode::Enabled, EFlowSignalMode::Disabled};
}

void UFlowNode_ExecutionMultiGate::ExecuteInput(const FName& PinName)
{
    if (PinName == DefaultInputPin.PinName)
    {
        if (Completed.Num() != OutputPins.Num())
        {
            Completed.Init(false, OutputPins.Num());
        }

        if (!Completed.Contains(false))
        {
            if (LoopCount > 0)
            {
                if (--LoopCount == 0)
                {
                    Finish();
                    return;
                }
            }
            else
            {
                Finish();
                return;
            }
        }

        const bool bUseStartIndex = !Completed.Contains(true) && Completed.IsValidIndex(StartIndex);

    	if (bParallel)
    	{
    		for (int32 CurrentOutput = 0; CurrentOutput < OutputPins.Num(); ++CurrentOutput)
    		{
    			if (!Completed[CurrentOutput])
    			{
    				const FString& OutputPinName = OutputPins[CurrentOutput].PinName.ToString();
    				UE_LOG(LogTemp, Warning, TEXT("Activating output: %s"), *OutputPinName);

    				Completed[CurrentOutput] = true;
    				TriggerOutput(OutputPinName, false);
    			}
    		}
    	}
    	
        else if (bRandom)
        {
            int32 Index;
            if (bUseStartIndex)
            {
                Index = StartIndex;
            }
            else
            {
                TArray<int32> AvailableIndexes;
                AvailableIndexes.Reserve(Completed.Num());

                for (int32 i = 0; i < Completed.Num(); i++)
                {
                    if (Completed[i] == false)
                    {
                        AvailableIndexes.Add(i);
                    }
                }

                if (AvailableIndexes.Num() == 0)
                {
                    return; // No available indexes, exit early
                }

                const int32 RandomIndex = FMath::RandRange(0, AvailableIndexes.Num() - 1);
                Index = AvailableIndexes[RandomIndex];
            }

            Completed[Index] = true;
            TriggerOutput(OutputPins[Index].PinName, false);
        }
        else
        {
            if (bUseStartIndex)
            {
                NextOutput = StartIndex;
            }

            const int32 CurrentOutput = NextOutput;
            NextOutput = (NextOutput + 1) % OutputPins.Num();

            Completed[CurrentOutput] = true;
            TriggerOutput(OutputPins[CurrentOutput].PinName, false);

            if (!Completed.Contains(false) && bLoop)
            {
                Finish();
            }
        }
    }
    else if (PinName == TEXT("Reset"))
    {
        Finish();
    }
}

void UFlowNode_ExecutionMultiGate::Cleanup()
{
	NextOutput = 0;
	Completed.Reset();
}

#if WITH_EDITOR
FString UFlowNode_ExecutionMultiGate::GetNodeDescription() const
{
	FString Result;
	Result.Reserve(128);

	if (bParallel)
	{
		Result.Append(TEXT("Parallel"));
		if (bLoop || bRandom) Result.Append(TEXT(", "));
	}
	
	if (bRandom)
	{
		Result.Append(TEXT("Random"));
		if (bLoop) Result.Append(TEXT(", "));
	}

	if (bLoop)
	{
		Result.Append(TEXT("Loop"));
	}

	if (StartIndex != INDEX_NONE)
	{
		if (bParallel || bRandom || bLoop)
		{
			Result.Append(TEXT(", "));
		}

		if (OutputPins.IsValidIndex(StartIndex))
		{
			Result.Appendf(TEXT("Start Index: %d"), StartIndex);
		}
		else
		{
			Result.Append(TEXT("StartIndex: Invalid"));
		}
	}

	return Result;
}
#endif
Leave a Comment