How do I use InstancedStaticMeshes?

#Instanced Static Mesh

You can make a new class, InstancedStaticMeshActor

and add a Instanced Static Mesh Component

Then you spawn that actor

and to add additional instances you use this function in the Instanced Static Mesh Component Class

UFUNCTION(BlueprintCallable, Category="Components|InstancedStaticMesh")
virtual void AddInstance(const FTransform& InstanceTransform);

That’s pretty much all you need to do!

Here’s my code from the beta, that I used for my own tests, here’s a pic of my tests

In my case I extended a regular SMA so I would see the actor when it spawned :slight_smile:

#Spawning

//Spawninfo
	FActorSpawnParameters SpawnInfo;

	//SET NO COLLISION FAIL TO TRUE
	SpawnInfo.bNoCollisionFail = true;
		
	SpawnInfo.Owner = VictoryEngine->VSelectedActor;
	
	AVictoryVertex3D* NewVertex = 
		GetWorld()->SpawnActor<AVictoryVertex3D>(
			AVictoryVertex3D::StaticClass(),
			Pos,
			FRotator::ZeroRotator,
			SpawnInfo 
	);
	if(!NewVertex) 							return NULL;
	if(!NewVertex->InstancedStaticMeshComponent) 	return NULL;
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	//Mesh
	NewVertex->InstancedStaticMeshComponent->SetStaticMesh(VictoryEngine->AssetSM_EngineCube);
		
	//Add Core Instance
	FTransform newT = NewVertex->GetTransform();
	newT.SetLocation(FVector(0,0,0));
	NewVertex->InstancedStaticMeshComponent->AddInstance(newT);
	
	//Scale
	NewVertex->SetActorRelativeScale3D(CurrentVerticiesScale);

#.h

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

//Power

#pragma once

#include "VictoryVertex3D.generated.h"


/**
 * An instance of a StaticMesh in a level
 * Note that PostInitializeComponents() is not called for StaticMeshActors
 */
UCLASS()
class AVictoryVertex3D : public AStaticMeshActor
{
	GENERATED_UCLASS_BODY()
			
	UPROPERTY()
	TSubobjectPtr<UInstancedStaticMeshComponent> InstancedStaticMeshComponent;
	
	
	UPROPERTY()
	UStaticMesh* SMAsset_Cube;
	
};

#.cpp

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#include "VictoryGame.h"

//////////////////////////////////////////////////////////////////////////
// VictoryVertex3D

//MAKE SEPARATE CLASSES OF THIS IF YOU WANT TO CHANGE THE MESH
//
AVictoryVertex3D::AVictoryVertex3D(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	//Mesh
	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_torus(TEXT("StaticMesh'/Engine/EngineMeshes/Cube.Cube'"));
	SMAsset_Cube = StaticMeshOb_torus.Object;
	
	//create new object
	InstancedStaticMeshComponent = PCIP.CreateDefaultSubobject < UInstancedStaticMeshComponent > (this, TEXT("InstancedStaticMeshComponentCOMP"));
	InstancedStaticMeshComponent.AttachTo(RootComponent);
	
	//Not Made some reason?
	if (!InstancedStaticMeshComponent) return;
	//~~~~~~~~~~~~~~~~~~~~~~~~
	
	//Set to Asset
	InstancedStaticMeshComponent->SetStaticMesh(SMAsset_Cube);
		
	InstancedStaticMeshComponent->bOwnerNoSee = false;
	InstancedStaticMeshComponent->bCastDynamicShadow = false;
	InstancedStaticMeshComponent->CastShadow = false;
	InstancedStaticMeshComponent->BodyInstance.SetObjectType(ECC_WorldDynamic);
	
	//Visibility
	InstancedStaticMeshComponent->SetHiddenInGame(false);
	
	//Mobility
	InstancedStaticMeshComponent->SetMobility(EComponentMobility::Movable);
	
	//Collision
	InstancedStaticMeshComponent->BodyInstance.SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	InstancedStaticMeshComponent->BodyInstance.SetObjectType(ECC_WorldDynamic);
	InstancedStaticMeshComponent->BodyInstance.SetResponseToAllChannels(ECR_Ignore);
	InstancedStaticMeshComponent->BodyInstance.SetResponseToChannel(ECC_WorldStatic, ECR_Block);
	InstancedStaticMeshComponent->BodyInstance.SetResponseToChannel(ECC_WorldDynamic, ECR_Block);
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	
	if (!StaticMeshComponent) return;
	
	//Mobility
	StaticMeshComponent->SetMobility(EComponentMobility::Movable);
	
	//Collision
	StaticMeshComponent->BodyInstance.SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	StaticMeshComponent->BodyInstance.SetObjectType(ECC_WorldDynamic);
	StaticMeshComponent->BodyInstance.SetResponseToAllChannels(ECR_Ignore);
	StaticMeshComponent->BodyInstance.SetResponseToChannel(ECC_WorldStatic, ECR_Block);
	StaticMeshComponent->BodyInstance.SetResponseToChannel(ECC_WorldDynamic, ECR_Block);
}