Instantiating GameObject / Spawning Actor

In Unity, we use the Instantiate function to create new instances of objects.

This function takes any UnityEngine.Object type (GameObject, MonoBehaviour, etc.), and makes a copy of it.

public GameObject EnemyPrefab; public Vector3 SpawnPosition; public Quaternion SpawnRotation; void Start() { GameObject NewGO = (GameObject)Instantiate(EnemyPrefab, SpawnPosition, SpawnRotation); NewGO.name = "MyNewGameObject"; }

In UE4, there are a couple different functions to instantiate objects, depending on your needs. NewObject is used for creating new UObject types, and SpawnActor is used for spawning AActor types.

First we will briefly talk about UObjects and NewObject. Subclassing UObject in Unreal is much like subclassing ScriptableObject in Unity. They are useful for gameplay classes that do not need to spawn into the world or have attached components like Actors do.

In Unity, if you created your own subclass of ScriptableObject, you may instantiate it like this:

MyScriptableObject NewSO = ScriptableObject.CreateInstance<MyScriptableObject>();

And in Unreal, if you created your own UObject derived type, you may instantiate it like this:

UMyObject* NewObj = NewObject<UMyObject>();

So what about Actors? Actors are spawned using the SpawnActor method on a World (UWorld in C++) object. How do you get the World object? Some UObjects provide a GetWorld method for you, as an example, all Actors do.

You will notice instead of passing in another Actor, we pass in the "class" of the Actor we want to spawn. In our example, the class can be any subclass of AMyEnemy.

But what if you want to make a "copy" of another object, like what Instantiate allows you to do?

The NewObject and SpawnActor functions can also be given a "template" object to work with. Unreal will make a copy of that object, instead of making one "from scratch". This will copy over all of its UPROPERTYs and components.

AMyActor* CreateCloneOfMyActor(AMyActor* ExistingActor, FVector SpawnLocation, FRotator SpawnRotation) { UWorld* World = ExistingActor->GetWorld(); FActorSpawnParameters SpawnParams; SpawnParams.Template = ExistingActor; World->SpawnActor<AMyActor>(ExistingActor->GetClass(), SpawnLocation, SpawnRotation, SpawnParams); }

You might be wondering what "from scratch" means in this context. Each object class you create has a default template that contain default values for its properties and components. If you do not override these properties and do not provide your own template, Unreal will use these default values to construct your object. To help illustrate this, let us first look at a MonoBehaviour for example:

public class MyComponent : MonoBehaviour { public int MyIntProp = 42; public SphereCollider MyCollisionComp = null; void Start() { // Create the collision component if we don't already have one if (MyCollisionComp == null) { MyCollisionComp = gameObject.AddComponent<SphereCollider>(); MyCollisionComp.center = Vector3.zero; MyCollisionComp.radius = 20.0f; } } }

In the above example, we have an int property that defaults to 42, and a SphereCollider component that defaults to a radius of 20.

We can achieve the same thing in Unreal using the object's constructor.

UCLASS() class AMyActor : public AActor { GENERATED_BODY() UPROPERTY() int32 MyIntProp; UPROPERTY() USphereComponent* MyCollisionComp; AMyActor() { MyIntProp = 42; MyCollisionComp = CreateDefaultSubobject<USphereComponent>(FName(TEXT("CollisionComponent")); MyCollisionComp->RelativeLocation = FVector::ZeroVector; MyCollisionComp->SphereRadius = 20.0f; } };

In the constructor of AMyActor, we have set the default property values for the class. Note the use of the CreateDefaultSubobject function. We can use it to create components and assign default properties to them. All the subobjects we create using this function act as a default template, so we can modify them in a subclass or Blueprint.