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