Download

AIController

hello,
I have recently tried to make a simple ai character with this tutorial: https://www.youtube.com/watch?v=P12uWSFVSIc&t=1795s
but when i hit the play button i get this error: UE4Editor.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000001604203000)
If anyone can give me a tip why that happens or even give me a simple ai controller script that would be great.
That’s the code of the ai controller c++:


// Fill out your copyright notice in the Description page of Project Settings.


#include "AI_Bot_Controller.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"

AAI_Bot_Controller::AAI_Bot_Controller()
{
    PrimaryActorTick.bCanEverTick = true;
    SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
    SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));

    AISightRadius = 500.0f;
    AISightAge = 5.0f;
    AILoseSightRadius = AISightRadius + 50.0f;
    AIFieldOfView = 90.0f;

    SightConfig->SightRadius = AISightRadius;
    SightConfig->LoseSightRadius = AILoseSightRadius;
    SightConfig->PeripheralVisionAngleDegrees = AIFieldOfView;
    SightConfig->SetMaxAge(AISightAge);

    SightConfig->DetectionByAffiliation.bDetectEnemies = true;
    SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
    SightConfig->DetectionByAffiliation.bDetectNeutrals = true;

    GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
    GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this,&AAI_Bot_Controller::OnPawnDetected);
    GetPerceptionComponent()->ConfigureSense(*SightConfig);

}

void AAI_Bot_Controller::BeginPlay()
{
    Super::BeginPlay();
    if (GetPerceptionComponent() != nullptr)
    {
        UE_LOG(LogTemp, Warning, TEXT("All System Set"));
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("Some Problem"));
    }
}

void AAI_Bot_Controller::OnPossess(APawn* Pawn)
{
    Super::Possess(Pawn);
}

void AAI_Bot_Controller::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
}

FRotator AAI_Bot_Controller::GetControlRotation() const
{
    if (GetPawn() == nullptr)
    {
        return FRotator(0.0f, 0.0f, 0.0f);
    }
    return FRotator(0.0f, GetPawn()->GetActorRotation().Yaw, 0.0f);
}

void AAI_Bot_Controller::OnPawnDetected(const TArray<AActor*>& DetectedPawns)
{
}


And the header file:


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "AIController.h"
#include "AI_Bot_Controller.generated.h"

/**
 * 
 */
UCLASS()
class THEMERCENARY_API AAI_Bot_Controller : public AAIController
{
    GENERATED_BODY()

public:
    AAI_Bot_Controller();

    virtual void BeginPlay() override;

    virtual void OnPossess(APawn* Pawn) override;

    virtual void Tick(float DeltaSeconds) override;

    virtual FRotator GetControlRotation() const override;

    UFUNCTION()
    void OnPawnDetected(const TArray<AActor*> &DetectedPawns);

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AI")
    float AISightRadius;

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AI")
    float AISightAge;

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AI")
    float AILoseSightRadius;

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AI")
        float AIFieldOfView;

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "AI")
    class UAISenseConfig_Sight* SightConfig;
};


Maybe you don’t have any PerceptionComponent?
try that before calling it:

*.cpp


if ensure(!GetPerceptionComponent()) {return;}

Why don’t you just use:


    PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComponent"));

I have edited the code to:


#include "AI_Bot_Controller.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"

AAI_Bot_Controller::AAI_Bot_Controller()
{
    PrimaryActorTick.bCanEverTick = true;
    SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
    PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComponent"));
    //SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));

    AISightRadius = 500.0f;
    AISightAge = 5.0f;
    AILoseSightRadius = AISightRadius + 50.0f;
    AIFieldOfView = 90.0f;

    SightConfig->SightRadius = AISightRadius;
    SightConfig->LoseSightRadius = AILoseSightRadius;
    SightConfig->PeripheralVisionAngleDegrees = AIFieldOfView;
    SightConfig->SetMaxAge(AISightAge);

    SightConfig->DetectionByAffiliation.bDetectEnemies = true;
    SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
    SightConfig->DetectionByAffiliation.bDetectNeutrals = true;

    GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
    GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this,&AAI_Bot_Controller::OnPawnDetected);
    GetPerceptionComponent()->ConfigureSense(*SightConfig);

}

void AAI_Bot_Controller::BeginPlay()
{
    Super::BeginPlay();
    if ensure(!GetPerceptionComponent()) { return; }
    if (GetPerceptionComponent() != nullptr)
    {
        UE_LOG(LogTemp, Warning, TEXT("All System Set"));
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("Some Problem"));
    }
}

void AAI_Bot_Controller::OnPossess(APawn* Pawn)
{
    Super::Possess(Pawn);
}

void AAI_Bot_Controller::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
}

FRotator AAI_Bot_Controller::GetControlRotation() const
{
    if (GetPawn() == nullptr)
    {
        return FRotator(0.0f, 0.0f, 0.0f);
    }
    return FRotator(0.0f, GetPawn()->GetActorRotation().Yaw, 0.0f);
}

void AAI_Bot_Controller::OnPawnDetected(const TArray<AActor*>& DetectedPawns)
{
}


with the same error

Important detail the fail accrues here in UObjectArray.cpp
(I put a break point in BeginPlay and it crushed before)


int32 FUObjectArray::AllocateSerialNumber(int32 Index)
{
    FUObjectItem* ObjectItem = IndexToObject(Index);
    checkSlow(ObjectItem);

    volatile int32 *SerialNumberPtr = &ObjectItem->SerialNumber;
    int32 SerialNumber = *SerialNumberPtr;
    if (!SerialNumber)
    {
        SerialNumber = MasterSerialNumber.Increment();
        UE_CLOG(SerialNumber <= START_SERIAL_NUMBER, LogUObjectArray, Fatal, TEXT("UObject serial numbers overflowed (trying to allocate serial number %d)."), SerialNumber);
        int32 ValueWas = FPlatformAtomics::InterlockedCompareExchange((int32*)SerialNumberPtr, SerialNumber, 0);
        if (ValueWas != 0)
        {
            // someone else go it first, use their value
            SerialNumber = ValueWas;
        }
    }
    checkSlow(SerialNumber > START_SERIAL_NUMBER);
    return SerialNumber;
}



Well now try replacing:



GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());

GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this,&AAI_Bot_Controller::OnPawnDetected);

GetPerceptionComponent()->ConfigureSense(*SightConfig); 

with:



  PerceptionComponent->SetDominantSense(*SightConfig->GetSenseImplementation());  

  PerceptionComponent->OnPerceptionUpdated.AddDynamic(this,&AAI_Bot_Controller::OnPawnDetected);

  PerceptionComponent->ConfigureSense(*SightConfig); 

Edit: Also change the BeginPlay() accordingly: replace all these GetPerceptionComponent() with PerceptionComponent

Still the same error its like something wrong with the stack size or something

Change this, likely causing your stackoverflow:


void AAI_Bot_Controller::OnPossess(APawn* Pawn) { **Super::Possess(Pawn);** }

to: Super::OnPossess(Pawn);

That was it!!!
thanks man it drove me crazy
:slight_smile: