Runtime generation of actors

Hi everyone,

I’m new to Unreal Engine 4 and can only use UE 4.8 so please bear this in mind with my question.
I want to generate three basic cubes (the meshes you find already in the engine) in the template first person shooter game they give you. Could someone help me or walk me through how I would go about this?

Thanks

Spawning an actor is pretty simple:

FActorSpawnParameters SpawnParams;
// set spawn params here

AStaticMeshActor* NewCube = GetWorld()->SpawnActor<AStaticMeshActor>( Location, Rotation, SpawnParams );

That will spawn you a static mesh actor, but you need to import the mesh you want to use. You’d do that in your constructor by first adding a UPROPERTY() to your class header:

UPROPERTY()
UStaticMesh* MyCubeMesh;

Then in the constructor doing this:

static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshFinder( TEXT( "static mesh reference, right click on the mesh in the content browser and do copy reference to get the actual link." ) );
MyCubeMesh = StaticMeshFinder.Object;

Then you use that class variable to set the static mesh on your new SM actor:

NewCube->SetStaticMesh( MyCubeMesh );

Thanks for your response and for the detail of it too. Sorry about the slow reply, but I have a couple of follow up questions:

  • Will this work at runtime? I’ve seen tutorials assigning them to key presses but I just want it to be instant.
  • Do I create a class from a static mesh and then use the code you suggested or do I split your suggestions between my already implemented game mode and a static mesh class?

Static meshes need a static mesh component to be rendered. Components need actors to be in the world. You can use any static mesh with Epic’s default ‘Static Mesh Actor’, which they’ve already set up to handle exactly this type of situation, or add a static mesh component to the actor of your choosing.

Yes, it works at runtime and even in the editor, with the right hooks. The code can go anywhere. I’d suggest either in the player controller or the player’s pawn. Either are good, as they are already set up to properly handle input. If you want it to be on key press that is. If you just want it to happen the second a map loads, HandleMatchIsWatingToStart() in the game mode is a great place.

Just applied your suggestions and it’s great to get such detail. I’ve still got some questions:

  • NewCube->SetStaticMesh( MyCubeMesh ); This brings up an error saying that my class (in this case it’s ASpawnedBlock) has no member SetStaticMesh.

  • `FActorSpawnParameters SpawnParams;
    // set spawn params here

AStaticMeshActor* NewCube = GetWorld()->SpawnActor( Location, Rotation, SpawnParams );` This section of code, I have put this into my ASpawnedBlock constructor along with the second and the other sections of code you have suggested. I’d copy them all but that would be a waste of time.Or would you suggest somewhere else?

Thanks for the help and sorry if my questions are becoming tedious haha

Ah. It’s NewCube->GetStaticMeshComponent()->SetStaticMesh( MyCubeMesh );

Also, constructors are run before there’s a world available. You can’t spawn or interact with any other objects in a constructor.

Am I correct in assuming you’ve created a subclass of AStaticMeshActor? Or AActor? And that is your ASpawnedBlock class?

Just tried that one, now it says GetStaticMeshComponent() has no member.

Ok, so if not in the constructor then where?
And I created a new Actor and just called it ASpawnedBlock. Was that incorrect?

Be useful if you pasted in all the code you’re using again.

This is why I suggested using a static mesh actor. Your actor has no static mesh component to assign your static mesh to.

I’ve moved the spawning params code to my game mode cpp file, the UPROPERTY and StaticMeshFinder code to my SpawnedCube cpp and h files. where does this line go however:
NewCube->SetStaticMesh( MyCubeMesh ); ?

And, importantly, is all that right? Because when i run the game a see a SpawnedCube in the World Outliner but it’s not visible (which i think is due to the line above still not working through incorrect placement or some other error)

GameMode.cpp:
ANewProject1GameMode::ANewProject1GameMode()
: Super()
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder PlayerPawnClassFinder(TEXT("/Game/FirstPerson/Blueprints/FirstPersonCharacter"));
DefaultPawnClass = PlayerPawnClassFinder.Class;

	// use our custom HUD class
	HUDClass = ANewProject1HUD::StaticClass();
}
void ANewProject1GameMode::BeginPlay(){

	FActorSpawnParameters SpawnParams;
	// set spawn params here

	FVector Location = FVector(-31.236938f, -99.205872f, 235.911285f);
	
	//(X = -31.236938, Y = -99.205872, Z = 235.911285)
	
	//FVector Rotation = 0.f, 0.f, 0.f;
	

		ASpawnedCube* NewCube = GetWorld()->SpawnActor<ASpawnedCube>(Location, FRotator::ZeroRotator, SpawnParams);
	


}

SpawnedCube.cpp

// Sets default values
ASpawnedCube::ASpawnedCube()
{
 	// 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;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshFinder(TEXT("StaticMesh'/Game/FirstPerson/Meshes/CubeMesh.CubeMesh'"));
	MyCubeMesh = StaticMeshFinder.Object;

}

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

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

SpawnedCube.h

UCLASS()
class NEWPROJECT1_API ASpawnedCube : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
ASpawnedCube();

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

// Called every frame
virtual void Tick( float DeltaSeconds ) override;

UPROPERTY()
	UStaticMesh* MyCubeMesh;

};

So create a class called StaticMeshActor which is an actor and then create a subclass of that? Sorry like I said I’m new to this so not 100% sure what you mean/meant

AStaticMeshActor is an engine class created by Epic.

You don’t add a component to it or use your spawnable cube class, you just do what i said originally. Spawn the static mesh actor, get the mesh component and set the mesh.

So how do I use the StaticMeshActor then? Add a component to it? Use my spawnable cube class?

Ok haha guess most of that was a waste. Is there any way you can show me in images? Might get it better if you can

Hey, sorry if this is an inconvenience to you but would you be able to show in my like annotated screenshots please? I think that would help me better understand it and to implement it. Probably should have stated that in my original question thinking about it.

http://puu.sh/mBuIl/6d31bae222.png

I appreciate the graphic, but i meant inside Unreal haha. sorry should have been more clear

You don’t need to do anything in the editor.