0

我在使用 KismetLibrary 在卷中生成随机点时遇到问题,

有代码

#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);
        }
    }
}

我应该能够在不使用 Kismet 的情况下生成随机点,但我认为如果存在这种库,我们应该使用它们......

它抛出以下错误:https ://i.stack.imgur.com/6nG45.png

编辑

在尝试调试时,我发现实际上是在我访问私有属性“SpawningVolume”时引发错误......仍然无法弄清楚为什么

这是 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();

};

编辑 2

刚刚添加了这个条件,似乎“SpawningVolume”是“nullptr”所以我不知道我做错了什么,但我一直在调查

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);
            }
        }
    }
}

错误

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]
4

0 回答 0