I would like to spawn just a simple, solid cube at a set location. I have tried many examples and have been through all of the questions/answers, but am still unable to do it.
Can someone please provide me with a full working example?
I have created a blueprint via the editor and called it ‘Cube-Blueprint’. I know I can get the reference to the blueprint and have tried to use it… unsuccessfully.
I have also tried without using the blueprint… and here it is:
Here is my actor .h
#pragma once
#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"
UCLASS()
class EARTHQUAKES_API AFloatingActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
AFloatingActor(const class FObjectInitializer& PCIP);
};
Here is my actor .cpp
#include "FloatingActor.h"
// Sets default values
AFloatingActor::AFloatingActor(const class FObjectInitializer& PCIP)
{
SetActorHiddenInGame(false);
}
In another class, within the BeginPlay(), I do this:
void AMapWall::BeginPlay()
{
Super::BeginPlay();
UWorld* const World = GetWorld();
if (World) {
for (int i = 0; i < 5; i++)
{
UStaticMesh* newComp = ConstructObject<UStaticMesh>(UStaticMesh::StaticClass(), this, FName("A_BOX"));
FVector Location = FVector(-746.0, 30, 50 * i);
AActor* actor = World->SpawnActor(AFloatingActor::StaticClass(), &Location);
actor->AddComponent(FName("A_BOX"), false, FTransform(), NULL);
}
}
}
I get my 5 actors created and visible in the “World Outliner” window, but they are not visible in the game and they do not have an mesh or anything else…
What am I doing wrong? Can I just instantiate the blueprint “Cube_Blueprint” that I created with the editor?