Hi,
I was trying to make a UBTTaskNode for a Behavior Tree, but I think I have done something wrong, probably with the includes, and I have not too much experience with this. I got the following errors:
C:\Documents\Unreal Projects\MyProject2 4.8\Source\MyProject2\buscarBomba.h(21): warning C4996: 'UBTTaskNode::ExecuteTask': This version is deprecated. Please use the one taking reference to UBehaviorTreeComponent rather than a pointer. Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.
C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\AIModule\Classes\BehaviorTree/BTTaskNode.h(109) : see declaration of 'UBTTaskNode::ExecuteTask'
c:\\documents\unreal projects\myproject2 4.8\source\myproject2\buscarBomba.h(21): warning C4996: 'UBTTaskNode::ExecuteTask': This version is deprecated. Please use the one taking reference to UBehaviorTreeComponent rather than a pointer. Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.
C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\AIModule\Classes\BehaviorTree/BTTaskNode.h(109) : see declaration of 'UBTTaskNode::ExecuteTask'
C:\Documents\Unreal Projects\MyProject2 4.8\Source\MyProject2\buscarBomba.cpp(31): warning C4996: FPostConstructInitializeProperties is deprecated and was renamed to FObjectInitializer. Please use that type instead.
C:\Documents\Unreal Projects\MyProject2 4.8\Source\MyProject2\buscarBomba.cpp(32): error C4273: 'UBTTaskNode::UBTTaskNode' : inconsistent dll linkage
C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\AIModule\Classes\BehaviorTree/BTTaskNode.h(29) : see previous definition of '{ctor}'
C:\Documents\Unreal Projects\MyProject2 4.8\Source\MyProject2\buscarBomba.cpp(38): error C4273: 'UBTTaskNode::ExecuteTask' : inconsistent dll linkage
C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\AIModule\Classes\BehaviorTree/BTTaskNode.h(109) : see previous definition of 'ExecuteTask'
C:\Documents\Unreal Projects\MyProject2 4.8\Source\MyProject2\buscarBomba.cpp(107): error C4273: 'UBTNode::GetStaticDescription' : inconsistent dll linkage
C:\Program Files\Epic Games\4.8\Engine\Source\Runtime\AIModule\Classes\BehaviorTree/BTNode.h(130) : see previous definition of 'GetStaticDescription'
My class.h file:
#pragma once
#include "BehaviorTree/BTTaskNode.h"
#include "buscarBomba.generated.h"
/**
*
*/
UCLASS()
class MYPROJECT2_API UbuscarBomba : public UBTTaskNode
{
GENERATED_BODY()
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent * OwnerComp, uint8* NodeMemory) override;
virtual FString GetStaticDescription() const override;
virtual float calculateFitness(FVector actorLocation);
};
and cpp:
#include "MyProject2.h"
#include "buscarBomba.h"
#include "AI_Character.h"
#include "globalBestData.h"
#include "MyProject2Character.h"
#include "Engine.h"
#include "Runtime/Core/Public/Math/UnrealMathUtility.h"
#include "Kismet/KismetMathLibrary.h"
#include "BehaviorTree/BTTaskNode.h"
float calculateFitness(FVector x){
AActor* objetivo;
for (TObjectIterator<AActor> Itr; Itr; ++Itr)
{
if (Itr->GetName() == "Target")
{
objetivo = *Itr;
break;
}
}
FVector posicionObjetivo = objetivo->GetActorLocation();
FVector aux = x - posicionObjetivo;
float length = aux.Size();
float fitness;
UNavigationSystem* NavSys = UNavigationSystem::GetCurrent(objetivo->());
NavSys->GetPathLength(x, posicionObjetivo, fitness);
return fitness;
}
UBTTaskNode::UBTTaskNode(const FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
NodeName = "buscarBomba";
}
EBTNodeResult::Type UBTTaskNode::ExecuteTask(UBehaviorTreeComponent* OwnerComp, uint8* NodeMemory)
{
AAI_Character* npc = Cast<AAI_Character>(OwnerComp->GetOwner());
bool buscaBomba = npc->isLeader;
bool objetivoAlcanzado = npc->goalReached;
bool haDetectadoEnemigo = npc->disparando;
FVector posicionActual = npc->GetActorLocation();
float diferenciaDeInercia = npc->inertiaDifference;
FVector velocidad = npc->particleVelocity;
float radioDeAceptacion = 425.0f;
float r1, r2;
float w = 0.7f;
float c1=1.4f, c2=1.4f;
if (buscaBomba && !objetivoAlcanzado && !haDetectadoEnemigo){
float fitness = calculateFitness(posicionActual);
if (fitness < npc->bestLocalFitness){
npc->bestLocalFitness = fitness;
npc->bestLocalPosition = posicionActual;
}
UglobalBestData* LoadGameInstance = Cast<UglobalBestData>(UGameplayStatics::CreateSaveGameObject(UglobalBestData::StaticClass()));
LoadGameInstance = Cast<UglobalBestData>(UGameplayStatics::LoadGameFromSlot(LoadGameInstance->SaveSlotName, LoadGameInstance->UserIndex));
float bestGlobalFitness = LoadGameInstance->bestGlobalFitness;
FVector bestGlobalPosition = LoadGameInstance->bestGlobalPosition;
if (fitness < bestGlobalFitness){
bestGlobalFitness = fitness;
bestGlobalPosition = posicionActual;
UglobalBestData* SaveGameInstance = Cast<UglobalBestData>(UGameplayStatics::CreateSaveGameObject(UglobalBestData::StaticClass()));
SaveGameInstance->bestGlobalFitness = bestGlobalFitness;
SaveGameInstance->bestGlobalPosition = bestGlobalPosition;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, SaveGameInstance->UserIndex);
}
if (fitness <= radioDeAceptacion){
npc->goalReached = true;// Actualiza la variable ObjetivoAlcanzado
return EBTNodeResult::Succeeded;
}
else{
r1 = FMath::FRandRange(0.0f, 1.0f);
r2 = FMath::FRandRange(0.0f, 1.0f);
npc->inertiaDifference = fmodf(diferenciaDeInercia + 0.025f,w-0.025f);
npc->particleVelocity = velocidad*(w - npc->inertiaDifference)+(npc->bestLocalPosition - posicionActual)*r1*c1+r2*c2*(bestGlobalPosition - posicionActual);
posicionActual = posicionActual + npc->particleVelocity;
FRotator nuevaRotacionNPC = UKismetMathLibrary::FindLookAtRotation(npc->GetActorLocation(), posicionActual);
FRotator rotacionNPC = npc->GetActorRotation();
npc->SetActorRotation(FMath::RInterpTo(rotacionNPC, nuevaRotacionNPC, ()->GetDeltaSeconds(), 6.0f));
npc->GetCharacterMovement()->MaxWalkSpeed = 400;
npc->SetActorLocation(posicionActual);
return EBTNodeResult::Succeeded;
}
}
else{
return EBTNodeResult::Succeeded;
}
}
FString UBTNode::GetStaticDescription() const
{
return TEXT("Description");
}
Thank you in advance