Saturday, April 28, 2012

Using Archetypes in UnrealScript

The use of archetypes allows us to store an instance of a class to be modified and used as a template to create other instances of that class in the level editor. This feature is very handy for Designers who want to modify Actors in the level without relying entirely on the programmer.

Create an Archetype is simple, just select an Actor of the level, right-click and choose "Create Archetype ...", as seen in the image below:

Another way to create an archetype is through the window "Actor Classes". Select a class and right-click to see the option "Create Archetype...".

After that, a dialog box is displayed where you should enter the name of the new archetype and the package where it will be stored. The "Grouping" is optional.

After creation, the new archetype will be displayed in the "Content Browser". You can change its properties by clicking the right button on the Archetype and choosing "Properties...". To get the full name of the Archetype choose "Copy Full Name to Clipboard" and then paste in the desired place.

I created a class called ArchetypeSpawner to illustrate the use of archetypes in UnrealScript. This class is placeable and has a variable named archetypeReference which is used to refer to an archetype. There is a function that can be called by other classes to create an instance based on the archetype referenced. In this example it was created a 5-second Timer that will call this function once. The great advantage of this example using archetypes is that the variable is displayed in the editor for the Designer, so he can create any Archetype and inform the full name in the variable archetypeReference in the properties window, as seen in the image and code below.

class ArchetypeSpawner extends Actor

var() const archetype Actor archetypeReference;
//The use of the archetype modifier is optional.
event simulated PostBeginPlay()

function Timer()
    SpawnArchetypeAt( self.Location );

function Actor SpawnArchetypeAt( vector startLocation ) 
    local Actor archetypeSpawned;
    archetypeSpawned = Spawn(archetypeReference.Class, , , startLocation, , archetypeReference);
    return archetypeSpawned;


For more information about Archetypes: