in another thread user @Dom_Cabrinha implement a way to cycle through the indices without using Modulo, which I thought made way more sense.
I tried to implement this in C++, assuming it was a pretty simple if/else statement, but after compiling and running the game UE4 seems to just crash after it hits the max number. And I can’t really seem to figure out what is different between what I wrote vs what was on the Blueprint. Could anyone take a guess as to what is happening here…?
// Fill out your copyright notice in the Description page of Project Settings.
#include "ChooseNextWaypoint.h"
#include "AIController.h"
#include "PatrollingGuard.h" // TODO remove coupling
#include "BehaviorTree/BlackboardComponent.h"
EBTNodeResult::Type UChooseNextWaypoint::ExecuteTask(UBehaviorTreeComponent & OwnerComp, uint8 * NodeMemory)
{
// TODO protect against empty patrol routes
// Get patrol points
auto AIController = OwnerComp.GetAIOwner();
auto ControlledPawn = AIController->GetPawn();
auto PatrollingGuard = Cast<APatrollingGuard>(ControlledPawn);
auto PatrolPoints = PatrollingGuard->PatrolPointsCPP;
// Set next waypoint
//UE_LOG(LogTemp, Warning, TEXT("AI in C++"));
auto BlackboardComp = OwnerComp.GetBlackboardComponent();
auto Index = BlackboardComp->GetValueAsInt(IndexKey.SelectedKeyName);
BlackboardComp->SetValueAsObject(WaypointKey.SelectedKeyName, PatrolPoints[Index]);
// Cycle index
auto NextIndex = Index + 1;
if (NextIndex >= PatrolPoints.Num())
{
BlackboardComp->SetValueAsInt(IndexKey.SelectedKeyName, 0);
}
else
{
BlackboardComp->SetValueAsInt(IndexKey.SelectedKeyName, NextIndex);
}
UE_LOG(LogTemp, Warning, TEXT("Waypoint index: %i"), Index);
return EBTNodeResult::Succeeded;
}
this by the way is what I was trying to follow: