UDN
Search public documentation:

DevelopmentKitGemsAddingSpritesMeshesParticleEffects
日本語訳
中国翻译
한국어

Interested in the Unreal Engine?
Visit the Unreal Technology site.

Looking for jobs and company info?
Check out the Epic games site.

Questions about support via UDN?
Contact the UDN Staff

Unreal Development Kit Home > Unreal Development Kit Gems > How to add sprites, meshes or particle effects to an actor

How to add sprites, meshes or particle effects to an actor


Last tested against UDK Mar, 2011
PC and iOS compatible

Overview


In Unreal Engine 2, actors could only render one thing. Thus if you wanted an actor to be represented by several meshes or a mixture of a mesh and a particle effect you would have to synchronize multiple actors. Unreal Engine 3 solves this by splitting these tasks out into actor components. Actor components are objects within actors that perform a specific task.

Thus in order for an actor to render itself as a static mesh, it must add a StaticMeshComponent. Or a SkeletalMeshComponent. Or a SpriteComponent. Or a ParticleSystemComponent. And so forth. However, it doesn't have to be one or the other. You can have as many actor components as you want within an actor, and it's also possible for them to interact with one another.

This gem will show you how to use actor components within actors.

Related topics

Unrealscript method


Adding actor components in Unrealscript is done in two ways. You can either declare them as sub objects in the default properties or create them during run time. In the following examples, Static Mesh Component was used, however you can replace this with other visual components such as Skeletal Mesh Component, Sprite Component or Particle System Component.

Default properties

How to add a static mesh sub object in the default properties within Unrealscript.

First we declare a static mesh component variable within the class global variable list. This ensures that the class has a reference to the static mesh component defined within the default properties.

Within the default properties we create a sub object. After the sub object is created within the default properties, it is added to the component array within the actor. All component sub objects should be added to the component array or they won't be used.

MyActor.uc
class MyActor extends Actor;

// Expose to Unrealscript and Unreal Editor
var() StaticMeshComponent StaticMesh;

defaultproperties
{
  // Declare sub object
  Begin Object Class=StaticMeshComponent Name=MyStaticMeshComponent
    StaticMesh=StaticMesh'EditorMeshes.TexPropPlane'
  End Object
  StaticMesh=MyStaticMeshComponent
  Components.Add(MyStaticMeshComponent)
}

If we wish to redefine a sub object that was previously declared within a parent class, we would do it like so. The only change is that you do not need to declare the class parameter. This is useful when you want to redefine some properties, which in this case a new static mesh definition.

MySubActor.uc
class MySubActor extends MyActor;

defaultproperties
{
  // Redefine sub object
  Begin Object Name=MyStaticMeshComponent
    StaticMesh=StaticMesh'EngineMeshes.Cube'
  End Object
  StaticMesh=MyStaticMeshComponent
  Components.Add(MyStaticMeshComponent)
}

If you wish to remove a previously declared component from your sub class.

MySubActor.uc
class MySubActor extends MyActor;

defaultproperties
{
  Components.Remove(MyStaticMeshComponent) // Remove a specific actor component
  Components.Empty // Remove all actor components
}

Run time

How to add a static mesh component in Unrealscript during run time. This can be added in any function and not just in PostBeginPlay. You may need to initialize the actor component or set up some other properties.

MyActor.uc
class MyActor extends Actor;

var() StaticMeshComponent StaticMesh;

function PostBeginPlay()
{
  Super.PostBeginPlay();

  StaticMesh = new () class'StaticMeshComponent';

  if (StaticMesh != None)
  {
    StaticMesh.SetStaticMesh(StaticMesh'EngineMeshes.Cube');
    AttachComponent(StaticMesh);
  }
}

defaultproperties
{
}

Related topics

Unreal Editor method


How to create an editor friendly version. A downside to this version is that actors using this won't be able to visualize what the actor will look like within the editor without running the game. Although this is alleviated by simply running PIE.

Adding the editinline and instanced variable modified to the primitive component variable declaration does two things. First the editinline variable modifier allows the editor to edit the primitive component within the properties window. Secondly the instanced variable modifier means that the object reference will automatically be recreated when the actor is spawned.

DynamicActorComponent.uc
class DynamicActorComponent extends Actor
   ClassGroup(Common)
   AutoExpandCategories(DynamicActorComponent)
   placeable;

// Expose to Unrealscript and Unreal Editor
var() const EditInline Instanced array<PrimitiveComponent> PrimitiveComponents;

function PostBeginPlay()
{
  local int i;

  // Check the primitive components array to see if we need to add any components into the components array.
  if (PrimitiveComponents.Length > 0)
  {
    for (i = 0; i < PrimitiveComponents.Length; ++i)
    {
      if (PrimitiveComponents[i] != None)
      {
        AttachComponent(PrimitiveComponents[i]);
      }
    }
  }

  Super.PostBeginPlay();
}

defaultproperties
{
  Begin Object Class=SpriteComponent Name=Sprite
    Sprite=Texture2D'EditorResources.S_Actor'
   HiddenGame=True
   AlwaysLoadOnClient=False
   AlwaysLoadOnServer=False
  End Object
  Components.Add(Sprite)
}

Using the DynamicActorComponent array

Open up the Content Browser and click on the Actor Classes tab. Your new DynamicActorComponent should be within the Common group. Select it.

AddingComponents_0_SelectDynamicActorComponent.jpg

Make a simple BSP floor within the world. Remember to add a light actor to the world or the scene won't be lit properly.

AddingComponents_1_AddBSPFloor.jpg

Right click within the world viewport where you want to place the dynamic actor component. It will open up the context menu. Select and click "Add DynamicActorComponent Here".

AddingComponents_2_AddDynamicActorComponent.jpg

When it created it will automatically be selected.

AddingComponents_3_AddedDynamicActorComponent.jpg

Press F4 to open the properties window.

AddingComponents_5_DynamicActorComponentProperties.jpg

Or, if it isn't selected you always find the actor within the actor list.

AddingComponents_4_SearchForDynamicActorComponent.jpg

Click on the green plus symbol within the "Primitive Components" array. This will create a new entry within the array. Then click on the blue down array and select StaticMeshComponent to create a new StaticMeshComponent object.

AddingComponents_6_AddingNewPrimitiveComponent.jpg

Find and select an interesting static mesh within the Content Browser.

AddingComponents_7_FindAndSelectCoolStaticMesh.jpg

Expand the new object definition. And then the Static Mesh Component tab. Press the small green arrow to set the static mesh.

AddingComponents_8_SetStaticMesh.jpg

Start up PIE to visual the actor.

AddingComponents_9_PIE.jpg

Downloads


  • Download the content used in the examples above. (DynamicActorComponents.zip)