How to implement Non-Modulo method from Blueprint?

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:

Could you post the crash log?

Hi Daniel, i tried this out again after completing lecture 248 and this time it didnt crash for whatever reason.

so i tried going back to what i had at the end of lecture 247 (in Source Tree i checked out the commit i made at the end of watching the video), used git clean -x -d -i to make sure i cleaned up the project, built a new Visual Studio 2017 solution file, and i compiled with the non-modular way of cycling the indices and it still didnt crash!

I don’t understand what changed? Is there a way i can grab the last generated crash log file some how…?

Well it’s probably deleted now with git clean. But they would be in Saved > Crashes.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms