I have made a pickup actor and a spawner actor. I after 2 DAYS of debugging and researching I managed to actually spawn the actor but the static mesh does not spawn it with it. Please help!
Parameters of the problem:
1.Mesh non-existent (It is not just invisible)
2.Actor spawns in normally
3.I am new to unreal, accustomed to C++
Code:
Pickup.h
#pragma once
#include "Components/BoxComponent.h"
#include "Components/StaticMeshComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h"
UCLASS()
class COOKBOOK_API APickup : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APickup();
UPROPERTY(EditAnywhere)
UStaticMeshComponent* PickupMesh;
UPROPERTY(EditAnywhere)
UBoxComponent* PickupBox;
UFUNCTION()
void OnPickedUp(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Pickup.cpp
#include "Pickup.h"
// Sets default values
APickup::APickup()
{
// 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;
PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>("PickupMesh");
RootComponent = PickupMesh;
PickupBox = CreateDefaultSubobject<UBoxComponent>("PickupBox");
PickupBox->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPickedUp);
}
void APickup::OnPickedUp(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
GLog->Log("Picked Up Pickup!");
Destroy();
}
// Called when the game starts or when spawned
void APickup::BeginPlay()
{
Super::BeginPlay();
FVector size(FMath::RandRange(0.3f, 1.0f));
PickupMesh->SetWorldScale3D(size);
PickupBox->SetWorldScale3D(size * 1.1);
PickupBox->AttachToComponent(PickupMesh, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
// Called every frame
void APickup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
PickupSpawner.h
#pragma once
#include "Pickup.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PickupSpawner.generated.h"
UCLASS()
class COOKBOOK_API APickupSpawner : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APickupSpawner();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
USceneComponent* Scene;
void SpawnPickup();
UPROPERTY(EditAnywhere)
TSubclassOf<class APickup> PickupToSpawn;
};
PickupSpawner.cpp
#include "PickupSpawner.h"
#include "Kismet/KismetMathLibrary.h"
#include "Public/TimerManager.h"
#include "Engine/World.h"
// Sets default values
// Sets default values
APickupSpawner::APickupSpawner()
{
// 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;
Scene = CreateDefaultSubobject<USceneComponent>("Scene");
}
// Called when the game starts or when spawned
void APickupSpawner::BeginPlay()
{
Super::BeginPlay();
FTimerHandle DummyHandle;
GetWorldTimerManager().SetTimer(DummyHandle, this, &APickupSpawner::SpawnPickup, 3.f, true);
}
// Called every frame
void APickupSpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void APickupSpawner::SpawnPickup()
{
FTransform SpawnTransform = Scene->GetComponentTransform();
FVector SpawnLocation = SpawnTransform.GetLocation();
SpawnLocation.X += FMath::RandRange(-200.0f, 200.0f);
SpawnTransform.SetLocation(SpawnLocation);
GetWorld()->SpawnActor<APickup>(APickup::StaticClass(), SpawnTransform);
GLog->Log("Spawned the UsefulActor.");
}