Download

Spawning instanced static mesh in code

I am pulling my hair out right now. Nobody seems to want to answer this question on the “AnswerHub” so I’m posting it here.

I have several StaticMesh variables that I need to spawn into the world via CPP code and NOTHING I am attempting to do is working. I cannot believe something like this is seemingly so impossible to do!

PLEASE! This is my last hurdle to getting my prototype up and running and I’m at my wit’s end. I’m going through the docs and trying every variation of SpawnActor that I can think of, and NOTHING is working for me. I get nothing to actually spawn (that is when I’m not actually getting compile errors due to my usage, which I HAVE NO IDEA BECAUSE I’M NOT FINDING ANY USEFUL INFORMATION!!)

Forgive me if I seem a bit upset, but I am. This seems like it should be one of the easiest things to accomplish, so why have I wasted a WEEK trying to get a simple mesh spawned into the world at a specific location through C++ code without success??

I have a UStaticMesh * mesh; variable that I need to spawn many instances of at different locations. What magical functions am I missing?

Hi Mbublitz sorry about that, but yes indeed spawn new actor is a task very simple to do, i create this little sample for you



//my header
UFUNCTION(BlueprintCallable, Category = "hola", meta = (WorldContext = "WorldContextObject", UnsafeDuringActorConstruction = "true"))
		static AActor* spawn(UObject* WorldContextObject, TSubclassOf<AActor> actorClass, FVector Location, FRotator Rotation, bool bNoCollisionFail);


//my cpp
AActor* UMyBlueprintFunctionLibrary::spawn(UObject* WorldContextObject, TSubclassOf<AActor> actorClass, FVector Location, FRotator Rotation, bool bNoCollisionFail)
{
	AActor* newActor = NULL;
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
	if (World && *actorClass)
	{
		FActorSpawnParameters ActorSpawnParams;
		ActorSpawnParams.SpawnCollisionHandlingOverride = bNoCollisionFail ? ESpawnActorCollisionHandlingMethod::AlwaysSpawn : ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;

		newActor = World->SpawnActor<AActor>(*actorClass, Location, Rotation, ActorSpawnParams);

	
	}

	return newActor;
}


this is a copy from spawn actor by class blueprint, as you can see you just need a world instance to call SpawnActor, and really the only line to do the trick is this:
newActor = World->SpawnActor<AActor>(AActor::StaticClass(), Location, Rotation, ActorSpawnParams);

if you have question see the documentation about: Spawning Actors | Unreal Engine Documentation spawn actor is very well doc
we have a lot of ways to instance a new actor.

I might be mistaken however i think you have to attach the mesh to an AActor and spawn the AActor.



AKAsset* SpawnedActor1 = (AKAsset*) GetWorld()->SpawnActor(MYAActor::StaticClass(), NAME_None, &Location);


Location is the FVector where you want to spawn your actor.

here is a basic header for the MYAActor file.



#include "GameFramework/Actor.h"
#include "MYAActor.generated.h"

UCLASS()
class GAME_API MYAActor : public AActor
{
	GENERATED_UCLASS_BODY() //note that this is GENERATED_UCLASS_BODY and not GENERATED_BODY.
	
public:
    UStaticMeshComponent* Mesh;
    
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	
	
};


MYAActor would have a constructor like:



MYAActor::MYAActor(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
 	// 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> CMesh(TEXT("StaticMesh'/Game/sm/box.box'"));
    
    Mesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("container")); // text("") can be just about anything.
    Mesh->SetStaticMesh(CMesh.Object);
    Mesh->AttachParent = RootComponent;
}


I have not found an easier way to do this at this point but hope this helps you solve your problem!

UE4 has AStaticMeshActor.

Use that.

Thanks everyone who responded to this and sorry I haven’t gotten back sooner. I think I’m thinking about this entirely the wrong way.

After pouring through the documentation again, I think what I really need to do is this:

My generator class is a child class of AActor. And I think what I need to do for all the geometry is create a static mesh component and set its StaticMesh to the particular level geometry I need for that particular spot and then add that to my generator class that gets dragged into the world. Does this sound about right? With the holidays I haven’t had much time to get back to this since posting the original question, but I’ve been reading in my spare time and so far this seems like the flow that I’m going to have to follow. Thoughts? Am I onto something here, or headed for another complete failure? lol