Difference between revisions of "Actor"

From UE4: Community Wiki
Jump to: navigation, search
m (fixing broken markup)
m (Spawn)
(2 intermediate revisions by the same user not shown)
Line 25: Line 25:
  
 
<syntaxhighlight lang="C++">
 
<syntaxhighlight lang="C++">
FActorSpawnParameters SpawnInfo;
+
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
+
SpawnInfo.Owner = this;
SpawnInfo.Instigator = GetInstigator();
+
SpawnInfo.Instigator = GetInstigator();
SpawnInfo.ObjectFlags |= RF_Transient;
+
SpawnInfo.ObjectFlags |= RF_Transient;
YourActorReference = GetWorld()->SpawnActor<AYourActor>(SpawnInfo);
+
YourActorReference = GetWorld()->SpawnActor<AYourActor>(SpawnInfo);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Spawning has the associated structure {{code|FActorSpawnParameters}} which is used to pass in a number of parameters to the Spawn function as in this example.
 
Spawning has the associated structure {{code|FActorSpawnParameters}} which is used to pass in a number of parameters to the Spawn function as in this example.
Line 42: Line 42:
 
</div>
 
</div>
 
=== Owner ===
 
=== Owner ===
{{todo|GetOwner()}}
+
Part of the Actor framework within Unreal Engine is the ability to get the Owner of an Actor, this is the Actor which either initiated the Spawn call or you can specify an Owner within the Spawn function. Getting the Owner is done through the {{code|GetOwner}} function defined in {{code|AActor}}.
 +
 
 +
<syntaxhighlight lang="C++">MyActorRef = GetOwner();</syntaxhighlight>
 +
 
 +
=== Instigator ===
 +
Sometimes you need to get the Actor which actually initiated a Spawn which can be different to the Actors Owner, this is where Instigator comes in. For example a Weapon might be the Owner of a Projectile but the Instigator is the Character which fired the Weapon, so when you {{code|GetInstigator}} on a Projectile it would return the Character.
 +
 
 +
<syntaxhighlight lang="C++">MyActorRef = GetInstigator();</syntaxhighlight>
  
 
== Destroy ==
 
== Destroy ==

Revision as of 21:07, 27 July 2020


This article is a preliminary version, it is subject to change.


Introduction

Actor is one of the most important aspects of Unreal Engine, it lays at the core of everything you will do interacting in the Unreal Editor viewport and the Game World. This core Unreal Engine Class AActor forms the basis for any Object you can see in the viewport in the 3d space, this does not include UMG widgets which are overlays.

Components

Actors are made up of various Components.

Info

There are a class of Actors which have very minimal presence in the game world, they are largely managers and extend from the Info Actor. See Game Framework explained for more.

Spawn

Spawning refers to the process of creating an Actor, similar to New Object though having a physical presence in the game world means Actors are treated differently, because they have components like collision. If an Actors Spawn location is being blocked we call that Encroachment, its when two Actors (or more) share the same physical space.

GetWorld()->SpawnActor<AYourActor>();

Since there is a requirement to having a game world we must first GetWorld to call Spawn on, this function is inherited by all Actors. This is the most basic implementation calling the default constructor.

FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
SpawnInfo.Instigator = GetInstigator();
SpawnInfo.ObjectFlags |= RF_Transient;
YourActorReference = GetWorld()->SpawnActor<AYourActor>(SpawnInfo);

Spawning has the associated structure FActorSpawnParameters which is used to pass in a number of parameters to the Spawn function as in this example.

GetWorld()->SpawnActor<AYourActor>(YourActorClass, SpawnInfo);

The above lets you specify a class with YourActorClass you may wish this to be editable on children of this class.


Spawn Actor API


Spawning Actors in C++

Owner

Part of the Actor framework within Unreal Engine is the ability to get the Owner of an Actor, this is the Actor which either initiated the Spawn call or you can specify an Owner within the Spawn function. Getting the Owner is done through the GetOwner function defined in AActor.

MyActorRef = GetOwner();

Instigator

Sometimes you need to get the Actor which actually initiated a Spawn which can be different to the Actors Owner, this is where Instigator comes in. For example a Weapon might be the Owner of a Projectile but the Instigator is the Character which fired the Weapon, so when you GetInstigator on a Projectile it would return the Character.

MyActorRef = GetInstigator();

Destroy

If you are finished with an Actor simply call the Destroy method.

YourActor->Destroy();
can bind delegates to spawn/destroy events

Archetypes

This structure FActorSpawnParameters gives us an opportunity to use another existing Actor as a Template for spawning, as shown below with YourTemplateActor reference being set to use when Spawned.

	FActorSpawnParameters SpawnInfo;
	SpawnInfo.Owner = this;
	SpawnInfo.Template = YourTemplateActor;
	YourActorReference = GetWorld()->SpawnActor<AYourActor>(SpawnInfo);
FActorSpawnParameters

Attachment

Attaching Actors typically happens through their Components, the most basic form of attachment would be through two Actors Root Components using AttachToActor.

AttachToActor(ParentActor);

You can also optionally use the named socket or FAttachmentTransformRules.

AttachToActor(ParentActor, FAttachmentTransformRules, SocketName);
AttachToComponent(USceneComponent * Parent, FAttachmentTransformRules, SocketName);

To check to see if your attachment worked.

if(YourActor->IsAttachedTo(ParentActor)){}
USceneComponent* AttachComponent = YourActor->GetDefaultAttachComponent();
TArray <AActor*> YourOutActors,
YourActor->GetAttachedActors(&YourOutActors, false);

YourOutActors array is now filled with a list of all Actors attached to YourActor.

ParentActor = YourActor->GetAttachParentActor();

Detachment

YourActor->DetachFromActor(FDetachmentTransformRules);

Detaches the RootComponent of this Actor from any SceneComponent it is currently attached to.

YourActor->DetachAllSceneComponents(ParentComponent, FDetachmentTransformRules);

Sockets

FName  	GetAttachParentSocketName()

Editor

FText* Reason = "";
if(YourActor->EditorCanAttachTo(YourParent, Reason){}

Replication

Actor Channels

Further Reading

Actors Architecture