Hi,
you’d need to create your own StateTreeAIComponent, you can just open unreal engine, click on your C++ folder, right click, and create new C++ class, in the search you can type StateTreeAIComponent, or if its not for AI, just StateTreeComponent. Click on that, name your Class, like MyProjectStateTreeAiComponent
.
Now with the new class created, you’d need some boilerplate code, so it works properly, in this case you’ll need at least the Constructor, BeginPlay and Tick function, from the Parent component we inherited(StateTreeAIComponent).
it would look something like this in your MyProjectStateTreeAiComponent.h
file:
#pragma once
#include "CoreMinimal.h"
#include "Components/StateTreeAiComponent.h"
#include "MyProjectStateTreeComponent.generated.h"
/**
*
*/
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class MYPROJECT_API UMyProjectStateTreeAiComponent: public UStateTreeAIComponent
{
GENERATED_BODY()
public:
UMyProjectStateTreeAiComponent();
protected:
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(BlueprintCallable, Category="MyProject")
void StartLogicWithSelectedStateTree(UStateTree* StateTree);
};
also if you want to include the logic from the previous post to Start Logic with a different StateTree Asset, then also add the last function above StartLogicWithSelectedStateTree
and create the definitions in the .cpp file:
#include "MyProjectStateTreeComponent.h"
UMyProjectStateTreeAiComponent::UMyProjectStateTreeAiComponent()
{
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.bStartWithTickEnabled = true;
SetStartLogicAutomatically(false);
}
void UMyProjectStateTreeAiComponent::BeginPlay()
{
Super::BeginPlay();
}
void UMyProjectStateTreeAiComponent::TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
void UMyProjectStateTreeAiComponent::StartLogicWithSelectedStateTree(UStateTree* StateTree)
{
if (StateTree)
{
if (IsRunning())
{
Cleanup();
}
StateTreeRef.SetStateTree(StateTree);
StartLogic();
}
}
Then you should add your component to the Ai Controller or whatever actor you want to use it on, and when you then drag the component in and call StartLogicWithSelectedStateTree
, it will give you the option to select a StateTree Asset and will execute the logic above!
Just tried implementing it myself and also had to add "StateTreeModule"
to my MyProject.Build.cs
file:
PublicDependencyModuleNames.AddRange(
new string[] {
"Core",
"CoreUObject",
"Engine",
"GameplayTags",
"Niagara",
"NavigationSystem",
"AIModule",
// ... all your other modules ...
"StateTreeModule" // add this
});