Difference between revisions of "Actor"

From UE4: Community Wiki
Jump to: navigation, search
m (Spawn)
m (Spawn)
 
(2 intermediate revisions by the same user not shown)
Line 39: Line 39:
 
{{epic|https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Actors/Spawning/index.html Spawning Actors in C++}}
 
{{epic|https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Actors/Spawning/index.html Spawning Actors in C++}}
 
</div><div id="BP" class="context-panel" style="display:none">
 
</div><div id="BP" class="context-panel" style="display:none">
 +
[[File:BlueprintNodeSpawnActor425.png|frame|none|Blueprint will not allow you to add a Spawn Actor node in a Class which doesnt have access to the world such as a Blueprint extending from Object.]]
 
{{epic|https://docs.unrealengine.com/en-US/Gameplay/HowTo/SpawnAndDestroyActors/Blueprints/index.html Spawning and Destroying Actors in Blueprint}}
 
{{epic|https://docs.unrealengine.com/en-US/Gameplay/HowTo/SpawnAndDestroyActors/Blueprints/index.html Spawning and Destroying Actors in Blueprint}}
 
</div>
 
</div>
 +
 
=== Owner ===
 
=== 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 {{code|GetOwner}} function defined in {{code|AActor}}.
 
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}}.
 
+
<div id="CPP" class="context-panel" style="display:block">
 
<syntaxhighlight lang="C++">MyActorRef = GetOwner();</syntaxhighlight>
 
<syntaxhighlight lang="C++">MyActorRef = GetOwner();</syntaxhighlight>
 +
</div>
 +
<div id="BP" class="context-panel" style="display:none">
 +
[[File:BlueprintNodeGetOwner425.png|frame|none|Returns the Owning Actor of a Component.]]
 +
[[File:BlueprintNodeGetOwnerSceneRoot425.png|frame|none|Usage is like this, used on a Root Component if you want to get the Owner of the Actor.]]
 +
</div>
  
 
=== Instigator ===
 
=== 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.
 
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.
 
+
<div id="CPP" class="context-panel" style="display:block"><syntaxhighlight lang="C++">MyActorRef = GetInstigator();</syntaxhighlight></div>
<syntaxhighlight lang="C++">MyActorRef = GetInstigator();</syntaxhighlight>
+
<div id="BP" class="context-panel" style="display:none">
 +
[[File:BlueprintNodeGetInstigator425.png|frame|none|Returns the Instigator Actor.]]
 +
[[File:BlueprintNodeGetInstigatorController425.png|frame|none|Returns the Controller for the Instigator Actor.]]
 +
</div>
  
 
== Destroy ==
 
== Destroy ==
 
If you are finished with an Actor simply call the {{code|Destroy}} method.
 
If you are finished with an Actor simply call the {{code|Destroy}} method.
<div id="CPP" class="context-panel" style="display:block">
+
<div id="CPP" class="context-panel" style="display:block"><syntaxhighlight lang="C++">YourActor->Destroy();</syntaxhighlight></div>
<syntaxhighlight lang="C++">YourActor->Destroy();</syntaxhighlight>
+
<div id="BP" class="context-panel" style="display:none">[[File:BlueprintNodeDestroyActor425.png|frame|none|Called for any Actor which is selected as the Target]]</div>
</div><div id="BP" class="context-panel" style="display:none">
+
 
</div>
 
 
{{todo|can bind delegates to spawn/destroy events}}
 
{{todo|can bind delegates to spawn/destroy events}}
 
== Archetypes ==
 
== Archetypes ==
Line 62: Line 71:
 
This structure {{code|FActorSpawnParameters}} gives us an opportunity to use another existing Actor as a Template for spawning, as shown below with {{code|YourTemplateActor}} reference being set to use when Spawned.
 
This structure {{code|FActorSpawnParameters}} gives us an opportunity to use another existing Actor as a Template for spawning, as shown below with {{code|YourTemplateActor}} reference being set to use when Spawned.
 
<syntaxhighlight lang="C++">
 
<syntaxhighlight lang="C++">
FActorSpawnParameters SpawnInfo;
+
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
+
SpawnInfo.Owner = this;
SpawnInfo.Template = YourTemplateActor;
+
SpawnInfo.Template = YourTemplateActor;
YourActorReference = GetWorld()->SpawnActor<AYourActor>(SpawnInfo);
+
YourActorReference = GetWorld()->SpawnActor<AYourActor>(SpawnInfo);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
{{epic|https://docs.unrealengine.com/en-US/API/Runtime/Engine/Engine/FActorSpawnParameters/index.html FActorSpawnParameters}}
 
{{epic|https://docs.unrealengine.com/en-US/API/Runtime/Engine/Engine/FActorSpawnParameters/index.html FActorSpawnParameters}}
 
</div>
 
</div>
 +
 
== Attachment ==
 
== Attachment ==
 
<div id="CPP" class="context-panel" style="display:block">
 
<div id="CPP" class="context-panel" style="display:block">

Latest revision as of 23:04, 27 July 2020


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


Introduction[edit]

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[edit]

Actors are made up of various Components.

Info[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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

YourActor->Destroy();


can bind delegates to spawn/destroy events

Archetypes[edit]

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[edit]

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[edit]

YourActor->DetachFromActor(FDetachmentTransformRules);

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

YourActor->DetachAllSceneComponents(ParentComponent, FDetachmentTransformRules);

Sockets[edit]

FName  	GetAttachParentSocketName()

Editor[edit]

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

Replication[edit]

Actor Channels[edit]

Further Reading[edit]

Actors Architecture