Skip to content

Instantly share code, notes, and snippets.

@Nuclearfossil
Created January 4, 2018 06:02
Show Gist options
  • Save Nuclearfossil/579c3db85f9b9c36dd199e55b2930da5 to your computer and use it in GitHub Desktop.
Save Nuclearfossil/579c3db85f9b9c36dd199e55b2930da5 to your computer and use it in GitHub Desktop.
Spawning an existing Actor derived object (Unreal)
// Fill out your copyright notice in the Description page of Project Settings.
#include "StaticMeshSpawner.h"
#include "UObject/ConstructorHelpers.h"
#include "Engine/StaticMesh.h"
#include "Engine/CollisionProfile.h"
#include "Runtime/Engine/Classes/Components/InstancedStaticMeshComponent.h"
// Sets default values
AStaticMeshSpawner::AStaticMeshSpawner()
{
// 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 mesh (using the Matinee camera because I'm lazy and everyone has it)
static ConstructorHelpers::FObjectFinder<UStaticMesh> EditorCameraMesh(TEXT("/Engine/EditorMeshes/MatineeCam_SM"));
mProxyMesh = EditorCameraMesh.Object;
mProxyMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent0"));
mProxyMeshComponent->SetCollisionProfileName(UCollisionProfile::BlockAll_ProfileName);
mProxyMeshComponent->SetStaticMesh(mProxyMesh);
mProxyMeshComponent->Mobility = EComponentMobility::Static;
mProxyMeshComponent->bGenerateOverlapEvents = false;
mProxyMeshComponent->bUseDefaultCollision = true;
mProxyMeshComponent->bCastDynamicShadow = false;
mProxyMeshComponent->bCastStaticShadow = false;
mProxyMeshComponent->bIsEditorOnly = true;
RootComponent = mProxyMeshComponent;
mISMeshComponent = nullptr;
}
// Called when the game starts or when spawned
void AStaticMeshSpawner::BeginPlay()
{
mProxyMeshComponent->SetHiddenInGame(true);
if (mISMeshComponent == nullptr)
{
mISMeshComponent = NewObject<UInstancedStaticMeshComponent>(this);
mISMeshComponent->RegisterComponent();
mISMeshComponent->SetStaticMesh(MeshToSpawn);
mISMeshComponent->SetFlags(RF_Transactional);
AddInstanceComponent(mISMeshComponent);
}
FTransform NewTransform = this->GetTransform();
mISMeshComponent->AddInstance(NewTransform);
FTransform secondTransform(NewTransform.TransformPosition(FVector(0.0f, 100.0f, 0.0f)));
mISMeshComponent->AddInstance(secondTransform);
if (ActorToSpawn != nullptr)
{
FTransform thirdTransform(NewTransform.TransformPosition(FVector(0.0f, 100.0f, 0.0f)));
FActorSpawnParameters spawnParameters;
spawnParameters.Template = ActorToSpawn;
AActor* newActor = GetWorld()->SpawnActor<AActor>(ActorToSpawn->GetClass(), thirdTransform, spawnParameters);
newActor->SetOwner(GetOwner());
}
Super::BeginPlay();
}
// Called every frame
void AStaticMeshSpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "StaticMeshSpawner.generated.h"
class UStaticMeshComponent;
class UInstancedStaticMeshComponent;
class AActor;
UCLASS(HideCategories = ("Sprite", "Rendering", "Input", "Actor", "HLOD"))
class BBITESTBED_API AStaticMeshSpawner : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AStaticMeshSpawner();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
protected:
UPROPERTY(transient)
UStaticMesh* mProxyMesh;
UStaticMeshComponent* mProxyMeshComponent;
UInstancedStaticMeshComponent* mISMeshComponent;
public:
UPROPERTY(EditAnywhere, Category = "Spawn Mesh")
UStaticMesh* MeshToSpawn;
UPROPERTY(EditAnywhere, Category = "Spawn Actor")
// TSubclassOf<AActor> ActorToSpawn;
AActor* ActorToSpawn;
};
@Nuclearfossil
Copy link
Author

The ActorToSpawn property is the real thing I'm interested in. the MeshToSpawn is more for me to test out other things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment