简体   繁体   English

虚幻引擎 4 (ue4) 中具有自定义 c++ 蓝图 function 库的蓝图循环/for/while 节点

[英]Blueprint loop/for/while node with custom c++ blueprint function library in unreal engine 4 (ue4)

I need to create a custom blueprint node.我需要创建一个自定义蓝图节点。 I am using the blueprint function library.我正在使用蓝图 function 库。

The node will look like this:该节点将如下所示:

Input: int timedelayforeachloop int numberofloops输入: int timedelayforeachloop int numberofloops

output: exc loop exc completed output:exc循环exc完成

loop1.h循环1.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "loop1.generated.h"

/**
 * 
 */

UENUM(BlueprintType)
enum class EMultiBranchEnum1 : uint8
{
    BranchA,
    BranchB
};


UCLASS()
class MYPROJECT2_API Uloop1 : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
        UFUNCTION(BlueprintCallable, meta = (DisplayName = "loop", CompactNodeTitle = "2as2", ExpandEnumAsExecs = "Branches"), Category = "1")
        //UFUNCTION(BlueprintCallable, Meta = (DisplayName = "Multi Branch1", ExpandEnumAsExecs = "Branches"), Category = 1)
        static void multiBranch(EMultiBranchEnum1& Branches, int loopqty);
        //EMultiBranchEnum1::BranchB;

};

loop1.cpp循环1.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "loop1.h"

void Uloop1::multiBranch(EMultiBranchEnum1& Branches, int loopqty)
{

    int currloop1 = 0;
    int temp = 2;
    int i;
    for (i = 0; i < 10; i++){
        currloop1 = currloop1 + 1;
        Branches = EMultiBranchEnum1::BranchA;


    }

    if (temp > currloop1) {

        Branches = EMultiBranchEnum1::BranchB;
    }

    if(temp == 0) {

        Branches = EMultiBranchEnum1::BranchB;

    }


}

-- THE PROBLEM -- The for loop only runs the once (evident by the print node i have on branchA(It only prints a single time)) -- 问题 -- for 循环只运行一次(从我在 branchA 上的打印节点可以看出(它只打印一次))

-- What should happen with the code below -- the loop should run the 10 times (my print node should print 10 times) -- 下面的代码会发生什么 -- 循环应该运行 10 次(我的打印节点应该打印 10 次)

Instead of using UBlueprintFunctionLibrary , you should use UBlueprintAsyncActionBase .而不是使用UBlueprintFunctionLibrary ,您应该使用UBlueprintAsyncActionBase It will allow you to store state in the node and call things connected to the execution pins asynchronously.它将允许您将 state 存储在节点中,并异步调用连接到执行引脚的事物。

DelayLoop.h file:延迟循环.h 文件:

#include "CoreMinimal.h"
#include "Kismet/BlueprintAsyncActionBase.h"
#include "DelayLoop.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDelayOutputPin);

/**
 * 
 */
UCLASS()
class TEST_API UDelayLoop : public UBlueprintAsyncActionBase
{
    GENERATED_UCLASS_BODY()

public:
    UPROPERTY(BlueprintAssignable)
    FDelayOutputPin Loop;

    UPROPERTY(BlueprintAssignable)
    FDelayOutputPin Complete;

    UFUNCTION(BlueprintCallable, 
            meta = (BlueprintInternalUseOnly = "true", WorldContext = "WorldContextObject"), 
            Category = "Flow Control")
    static UDelayLoop* DelayLoop(const UObject* WorldContextObject, 
            const float DelayInSeconds, const int Iterations);

    virtual void Activate() override;

private:
    const UObject* WorldContextObject;
    float MyDelay;
    int MyIterations;
    bool Active;

    UFUNCTION()
    void ExecuteLoop();

    UFUNCTION()
    void ExecuteComplete();
};

DelayLoop.cpp file:延迟循环.cpp 文件:

#include "DelayLoop.h"
#include "Engine/World.h"
#include "TimerManager.h"

UDelayLoop::UDelayLoop(const FObjectInitializer& ObjectInitializer) : 
        Super(ObjectInitializer), WorldContextObject(nullptr), MyDelay(0.0f), 
        MyIterations(0), Active(false)
{
}

UDelayLoop* UDelayLoop::DelayLoop(const UObject* WorldContextObject, 
        const float DelayInSeconds, const int Iterations)
{
    UDelayLoop* Node = NewObject<UDelayLoop>();
    Node->WorldContextObject = WorldContextObject;
    Node->MyDelay = DelayInSeconds;
    Node->MyIterations = Iterations;
    return Node;
}


void UDelayLoop::Activate()
{
    if (nullptr == WorldContextObject)
    {
        FFrame::KismetExecutionMessage(TEXT("Invalid WorldContextObject."), 
                ELogVerbosity::Error);
        return;
    }
    if (Active)
    {
        FFrame::KismetExecutionMessage(TEXT("DelayLoop is already running."), 
                ELogVerbosity::Warning);
    }
    if (MyDelay <= 0.0f)
    {
        FFrame::KismetExecutionMessage(
                TEXT("DelayLoop delay can't be less or equal to 0."), 
                ELogVerbosity::Warning);
    }
    if (MyIterations <= 0)
    {
        FFrame::KismetExecutionMessage(
                TEXT("DelayLoop iterations can't be less or equal to 0."), 
                ELogVerbosity::Warning);
    }

    Active = true;
    for (int i = 0; i <= MyIterations; i++)
    {
        FTimerHandle IterationTimer;
        WorldContextObject->GetWorld()->GetTimerManager().SetTimer(
                IterationTimer, this, &UDelayLoop::ExecuteLoop, MyDelay * i);
    }

    FTimerHandle CompleteTimer;
    WorldContextObject->GetWorld()->GetTimerManager().SetTimer(
            CompleteTimer, this, &UDelayLoop::ExecuteComplete, 
            MyDelay * (MyIterations+1));
            // If the Complete pin should happen at the same time as the last iteration
            // use `MyDelay * MyIterations` here instead

}

void UDelayLoop::ExecuteLoop()
{
    Loop.Broadcast();
}

void UDelayLoop::ExecuteComplete()
{
    Complete.Broadcast();
    Active = false;
}

This will get you a blueprint that looks like this:这将为您提供如下所示的蓝图:

蓝图使用示例

Note: This code is heavily based on This Creating Asynchronous Blueprint Nodes guide by Daniel ~b617 Janowski, now hosted in the legacy wiki here注意:此代码很大程度上基于 Daniel ~b617 Janowski 的This Creating Asynchronous Blueprint Nodes 指南,现在托管在此处的旧 wiki 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM