# Runtime generation of actors

**URL:** <https://forums.unrealengine.com/t/runtime-generation-of-actors/343285>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [January 6, 2016, 4:45pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285 "2016-01-06T16:45:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 6, 2016, 4:45pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/1 "2016-01-06T16:45:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 6, 2016, 4:57pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/2 "2016-01-06T16:57:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 11, 2016, 6:59pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/3 "2016-01-11T18:59:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 11, 2016, 7:04pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/4 "2016-01-11T19:04:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 13, 2016, 5:49pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/5 "2016-01-13T17:49:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 13, 2016, 6:12pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/6 "2016-01-13T18:12:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 13, 2016, 6:22pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/7 "2016-01-13T18:22:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 16, 2016, 8:59pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/8 "2016-01-16T20:59:46Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 16, 2016, 9:05pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/9 "2016-01-16T21:05:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 16, 2016, 9:13pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/10 "2016-01-16T21:13:52Z")

</div>

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;

```

};

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 16, 2016, 9:27pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/11 "2016-01-16T21:27:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 16, 2016, 9:29pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/12 "2016-01-16T21:29:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 16, 2016, 10:06pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/13 "2016-01-16T22:06:42Z")

</div>

AStaticMeshActor is an engine class created by Epic.

> **[AStaticMeshActor](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Engine/AStaticMeshActor/)**
>
> StaticMeshActor is an instance of a \[UStaticMesh\](API\\Runtime\\Engine\\Engine\\UStaticMesh) in the world.

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 16, 2016, 10:15pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/14 "2016-01-16T22:15:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 16, 2016, 10:28pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/15 "2016-01-16T22:28:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 16, 2016, 11:35pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/16 "2016-01-16T23:35:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 19, 2016, 1:31pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/17 "2016-01-19T13:31:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 19, 2016, 1:36pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/18 "2016-01-19T13:36:19Z")

</div>

[http://puu.sh/mBuIl/6d31bae222.png](http://puu.sh/mBuIl/6d31bae222.png)

---

<div class="post-metadata">

**Author:** ![CJSTRAW93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CJSTRAW93](https://forums.unrealengine.com/u/CJSTRAW93)\
**Post date:** [January 19, 2016, 4:06pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/19 "2016-01-19T16:06:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 19, 2016, 4:43pm UTC](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285/20 "2016-01-19T16:43:57Z")

</div>

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

[Next page](https://forums.unrealengine.com/t/runtime-generation-of-actors/343285.md?page=2)
