Friday, October 14, 2011

ActorComponent (UnrealScript)

ActorComponent is a base class used for components that can be attached to an Actor. For example, we have a component that defines the 3D model (mesh) of an Actor. There are  components related to image, lighting, audio...

There is a way to create an instance of a component inside the class's DefaultProperties block, as we will see in the next code sample.

The first example will add a StaticMesh to an Actor using UnrealScript. Open the UDK ContentBrowser and choose a StaticMesh, write the Name and Path as seen in the image below.


Then create a simple class that extends Actor as follows:
class TestMesh extends Actor
      placeable;
     
defaultproperties
{
   begin object Class=StaticMeshComponent Name=Model3D
      StaticMesh = Pickups.UDamage.Mesh.S_Pickups_UDamage        
   end object
    
   Components.add(Model3D);
}

The commands "Begin Object / End Object" allows the creation of an object. We specify the class and the name of the object. Inside the block we can define the properties of the object. In this example we only define the path and name of the StaticMesh that will be used by the Actor. After that we added this new object to the list of Actor's components.

The image below shows our new Actor in the UDK editor:


The second example will create a SpriteComponent (2D image) for the Actor. In this example we want the image to be displayed in the editor but does not in the game. This is very useful for invisible objects that contains only AI logic, because with the image is easy to locate it in the editor.

Code sample and image:
class TestSprite extends Actor
      placeable;
     
defaultproperties
{
   begin object Class=SpriteComponent Name=Image2D
      Sprite = EditorResources.AIScript
      HiddenGame = true
   end object
    
   Components.add(Image2D);
}


For more information about Actor Components:
Actor Components