Skip to content

Instantly share code, notes, and snippets.

@Ciberusps
Last active April 19, 2024 12:04
Show Gist options
  • Save Ciberusps/667a6b694e35a1dbd3a17cedc66c6fc6 to your computer and use it in GitHub Desktop.
Save Ciberusps/667a6b694e35a1dbd3a17cedc66c6fc6 to your computer and use it in GitHub Desktop.
Unreal AI utils, Loop with random range, TimeLimit with random interval
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/Decorators/BTDecorator_LoopRandomCount.h"
UBTDecorator_LoopRandomCount::UBTDecorator_LoopRandomCount(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
NodeName = "Loop_RandomCount";
NumLoops = 3;
}
FString UBTDecorator_LoopRandomCount::GetStaticDescription() const
{
return FString::Printf(TEXT("Loops: %d-%d loops"), Range.Min, Range.Max);
}
void UBTDecorator_LoopRandomCount::OnNodeActivation(FBehaviorTreeSearchData& SearchData)
{
NumLoops = FMath::RandRange(Range.Min, Range.Max);
Super::OnNodeActivation(SearchData);
}
BTDecorator_LoopRandomCount.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/Decorators/BTDecorator_Loop.h"
#include "BTDecorator_LoopRandomCount.generated.h"
/**
*
*/
UCLASS(hidecategories=Decorator)
class BOGATYR_API UBTDecorator_LoopRandomCount : public UBTDecorator_Loop
{
GENERATED_BODY()
public:
UBTDecorator_LoopRandomCount(const FObjectInitializer& ObjectInitializer);
/** number of executions */
UPROPERTY(EditAnywhere, meta=(EditCondition="!bInfiniteLoop", ClampMin="1", ClampMax="255"))
FInt32Interval Range = FInt32Interval(2, 4);
virtual FString GetStaticDescription() const override;
protected:
virtual void OnNodeActivation(FBehaviorTreeSearchData& SearchData) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/Decorators/BTDecorator_TimeLimitRandomTime.h"
UBTDecorator_TimeLimitRandomTime::UBTDecorator_TimeLimitRandomTime(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
NodeName = "TimeLimit_RandomTime";
TimeLimit = 5.0f;
}
FString UBTDecorator_TimeLimitRandomTime::GetStaticDescription() const
{
// basic info: result after time
return FString::Printf(TEXT("TimeLimit: %s after %.1fs-%.1fs"),
*UBehaviorTreeTypes::DescribeNodeResult(EBTNodeResult::Failed), TimeLimitRange.Min, TimeLimitRange.Max);
}
void UBTDecorator_TimeLimitRandomTime::OnNodeActivation(FBehaviorTreeSearchData& SearchData)
{
TimeLimit = bUseIntegers
? FMath::RandRange((int32)TimeLimitRange.Min, (int32)TimeLimitRange.Max)
: FMath::RandRange(TimeLimitRange.Min, TimeLimitRange.Max);
Super::OnNodeActivation(SearchData);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/Decorators/BTDecorator_TimeLimit.h"
#include "BTDecorator_TimeLimitRandomTime.generated.h"
/**
*
*/
UCLASS(hidecategories=Decorator)
class BOGATYR_API UBTDecorator_TimeLimitRandomTime : public UBTDecorator_TimeLimit
{
GENERATED_BODY()
public:
UBTDecorator_TimeLimitRandomTime(const FObjectInitializer& ObjectInitializer);
/** TimeLimit */
UPROPERTY(EditAnywhere, meta=(ClampMin="0"))
FFloatInterval TimeLimitRange = FFloatInterval(3.0f, 5.0f);
UPROPERTY(EditAnywhere)
bool bUseIntegers = true;
virtual FString GetStaticDescription() const override;
protected:
virtual void OnNodeActivation(FBehaviorTreeSearchData& SearchData) override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment