Spawning Static Mesh Actors at runtime

Hello.
In my game I have static mesh actors that are stationary and hide after sustaining a certain amount of damage. I am quite new to UE4 and would like to spawn some static mesh actors at runtime. Is it possible to spawn stationary objects at runtime, and how do I spawn static meshes? I tried many functions but my editor keeps crashing or spawning an empty static mesh, but I would like mine to be the same model as I specify in the uproperty:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Ore Types”, meta = (AllowPrivateAccess = “true”))
UStaticMesh* Soil;

I want to spawn many statiuc meshes of one model. How do I do this?

1 Like

Hi

Yes it’s possible to spawn static meshes at runtime with mobility stationary.

This code below allows you to spawn 10 static mesh actors (for loop) for example with the StaticMesh defined in the editor.

In my .h :

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "MeshSpawner.generated.h"

UCLASS()
class MYPROJECT_API AMeshSpawner : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMeshSpawner();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Ore Types", meta = (AllowPrivateAccess = "true"))
	UStaticMesh* Soil;

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	void SpawnStaticMeshActor(const FVector &InLocation);
	
	
};

In my .cpp:

AMeshSpawner::AMeshSpawner()
{
 	// 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;

}

// Called when the game starts or when spawned
void AMeshSpawner::BeginPlay()
{
	Super::BeginPlay();
	
	for (int i=0; i<10; i++)
	{
		FVector ActorLocation = FVector(0.f, 0.f, i * 100.f);
		SpawnStaticMeshActor(ActorLocation);
	}
	
}

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

}

void AMeshSpawner::SpawnStaticMeshActor(const FVector &InLocation)
{
	AStaticMeshActor* MyNewActor = GetWorld()->SpawnActor<AStaticMeshActor>(AStaticMeshActor::StaticClass());
	MyNewActor->SetMobility(EComponentMobility::Stationary);
	MyNewActor->SetActorLocation(InLocation);
	UStaticMeshComponent* MeshComponent = MyNewActor->GetStaticMeshComponent();
	if (MeshComponent)
	{
		MeshComponent->SetStaticMesh(Soil);
	}
}

In my case, my function just takes the location as parameter, you can put whatever you want.

The function that spawns the actor is GetWorld()->SpawnActor()

This function can take an FTransform as an argument to set the location/rotation/scale …

Hope this helps you :slight_smile:

5 Likes

You can spawn stationary actors at runtime. The most common way to spawn any actor is:

AMyActor* NewMyActor  = GetWorld()->SpawnActor<AMyActor>();

So, too answer your question, add:

AStaticMeshActor* NewMesh = GetWorld()->SpawnActor<AStaticMeshActor>(FVector(0, 0, 0), FRotator(0, 0, 0));
NewMesh->SetStaticMesh(Soil);

The Vector is the location and the Rotator is the rotation.
If you are spawning a subclass of AStaticMeshActor, just substitute “AStaticMeshActor” for “AYourStaticMeshActor”

Hope this helps

1 Like

Thank you for this very thorough explanation, as it gives me a lot of insight into the process of spawning SMeshes.