EXCEPTION_ACCESS_VIOLATION when using RandomPointInBoundingBox method of KismetLibrary

141 Views Asked by At

I am facing an issue while generating a random point in a volume using KismetLibrary,

there is the code

#include "Spawner.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "BaseCell.h"

// Sets default values
ASpawner::ASpawner()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    SpawningVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnVolumeBox"));
    RootComponent = SpawningVolume;


    Cell = ABaseCell::StaticClass();
}

// Called when the game starts or when spawned
void ASpawner::BeginPlay()
{
    Super::BeginPlay();
    SpawnCells();
}

// Called every frame
void ASpawner::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

void ASpawner::SpawnCells()
{
    if(Cell != NULL)
    {
        UWorld* const World = GetWorld();

        if (World) {
            FVector Origin = SpawningVolume->Bounds.Origin;
            FVector Extent = SpawningVolume->Bounds.BoxExtent;

            FVector SpawnLocation = UKismetMathLibrary::RandomPointInBoundingBox(Origin, Extent);

            FRotator SpawnRotation;

            SpawnRotation.Pitch = FMath::FRand() * 360.0f;
            SpawnRotation.Roll = FMath::FRand() * 360.0f;
            SpawnRotation.Yaw = FMath::FRand() * 360.0f;

            ABaseCell* SpawnedCell = World->SpawnActor<ABaseCell>(Cell, SpawnLocation, SpawnRotation);
        }
    }
}

I should be able to generate without using Kismet the random point but i thought if this kind of library exists we should use them...

it's throwing the following error: https://i.stack.imgur.com/6nG45.png

EDIT

when trying to debug i figured that actually it's when i'm accessing private property "SpawningVolume" that error is thrown... still can't figure why

here is the Spawner.h

#include "BaseCell.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Spawner.generated.h"

UCLASS()
class LIFE_API ASpawner : public AActor
{
    GENERATED_BODY()
    
public: 
    // Sets default values for this actor's properties
    ASpawner();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Spawnable
    UPROPERTY(EditAnyWhere, category="Spawning")
    TSubclassOf<class ABaseCell> Cell;

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;

private:
    // SpawningVolume as BoxComponent
    UPROPERTY(VisibleAnyWhere, category="Spawning", meta=(AllowPrivateAccess="true"))
    class UBoxComponent* SpawningVolume;

    void SpawnCells();

};

EDIT 2

Just added this condition and it seems that "SpawningVolume" is "nullptr" so i don't know what i'm doing wrong but i keep investigating

void ASpawner::SpawnCells()
{
    if(Cell != NULL)
    {
        UWorld* const World = GetWorld();

        if (World) {
           // Added condition
            if (SpawningVolume != nullptr) {
                FVector Origin = SpawningVolume->Bounds.Origin;
                FVector Extent = SpawningVolume->Bounds.BoxExtent;

                FVector SpawnLocation = UKismetMathLibrary::RandomPointInBoundingBox(Origin, Extent);

                FRotator SpawnRotation;

                SpawnRotation.Pitch = FMath::FRand() * 360.0f;
                SpawnRotation.Roll = FMath::FRand() * 360.0f;
                SpawnRotation.Yaw = FMath::FRand() * 360.0f;

                ABaseCell* SpawnedCell = World->SpawnActor<ABaseCell>(Cell, SpawnLocation, SpawnRotation);
            }
        }
    }
}

ERROR

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000000000000012c

UE4Editor_Life_0001!ASpawner::SpawnCells() [C:\Users\Benja\OneDrive\Documents\Unreal Projects\Life\Source\Life\Spawner.cpp:46]
0

There are 0 best solutions below