Spawn empty actor

Hello, how i can spawn an empty actor using c++?

I’m trying to use this:

SpawnActor(AActor::GetClass(),GetActorLocation(),GetActorRotation());

but its seems to not working. I cant find object in object outliner and add components to it.

I don’t understand what you want to achieve with this empty actor, but I guess you can only spawn an actor class which inherits from AActor and is not the base class AActor itself. Try making a new actor class using File | Add Code To Project... in the Editor and choose AActor as the base class. In C++ use

()->SpawnActor<AMyActor>(AMyActor::StaticClass());

from e.g. your PlayerController or GameMode.

I will try it, thanks

It seems indeed

else if( !Class->IsChildOf(AActor::StaticClass()) )
{
	UE_LOG(LogSpawn, Warning, TEXT("SpawnActor failed because %s is not an actor class"), *Class->GetName() );
	return NULL;
}

IsChildOf probably return false on parent itself. Check the logs to confirm that

An empty actor can be usefull to act as source and target for a dynamically spawned Particle System and so on…

But if you really need one you need a Empty Actor class. AActor alone is not equipped with a scene component. So it can not be positioned in the scene.

Declare the AEmptyActor Class like this

#pragma once

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

UCLASS()
class YOURGAME_API AEmptyActor : public AActor
{
	GENERATED_BODY()
public:	
	AEmptyActor(const FObjectInitializer& ObjectInitializer);

	UPROPERTY()
	class USceneComponent* Scene;
};

and define it like that

#include "YourGame.h"
#include "EmptyActor.h"

AEmptyActor::AEmptyActor(const FObjectInitializer& ObjectInitializer) :
	Super(ObjectInitializer)
{
	PrimaryActorTick.bCanEverTick = false;
	
	Scene = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Scene"));
	Scene->SetMobility(EComponentMobility::Movable);
	SetRootComponent(Scene);
}

After that you can simply spawn it

()->SpawnActor(AEmptyActor::StaticClass());
1 Like