Try including the world.h header to use the GetWorld function.
#include "Engine/World.h"
Try including the world.h header to use the GetWorld function.
#include "Engine/World.h"
After following a tutorial, I got this code:
#include “PillSpawner.h”
#include “Components/BoxComponent.h”
#include “MagicPill.h”
#include “Classes/Kismet/KismetMathLibrary.h”
#include “Components/StaticMeshComponent.h”
// Sets default values
APillSpawner::APillSpawner()
{
// 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;
// This establishes the item to spawn as a Magic Pill
//ItemToSpawn = AMagicPill::StaticClass();
}
// Called when the game starts or when spawned
void APillSpawner::BeginPlay()
{
Super::BeginPlay();
SpawnPill();
}
// Called every frame
void APillSpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// This is a function that returns a random location in the volume
FVector APillSpawner::GetRandomPointInVolume() {
FVector Origin = SpawningVolume->Bounds.Origin;
FVector Extent = SpawningVolume->Bounds.BoxExtent;
return UKismetMathLibrary::RandomPointInBoundingBox(Origin, Extent);
}
void APillSpawner::SpawnPill() {
if (ItemToSpawn != NULL) {
UWorld* const World = GetWorld();
if (World) {
FVector SpawnLocation = GetRandomPointInVolume();
FRotator SpawnRotation;
SpawnRotation.Pitch = FMath::FRand() * 360.0f;
SpawnRotation.Roll = FMath::FRand() * 360.0f;
SpawnRotation.Yaw = FMath::FRand() * 360.0f;
AMagicPill* SpawnedPill = World->SpawnActor<AMagicPill>(ItemToSpawn, SpawnLocation, SpawnRotation);
}
}
}
Though, for some reason “World->SpawnActor” doesn’t work, since I get these errors:
E0393 pointer to incomplete class type is not allowed
E0254 type name is not allowed
I don’t have any errors at all on the MagicPill end, but there are multiple errors showing up on the Pill Spawner.
I’d really appreciate it if someone came with a solution or something that could help.
I assume that it has to do with what to include, but I can’t find anything related to include.
Well thank you! That worked, couldn’t seem to find the correct header file, so thanks for the help!
You are welcome, I’m glad I could help. If you could please mark my answer as correct to help others with a similar issue easily find the solution. Thank you