Click or drag to resize
DigitalRuneTemplates

This section describes the template mechanism of the game object system.

Templates

Templates (also called prefabs, prototypes, or archetypes) are prefabricated game objects. In games, templates are often defined in a game editor and loaded at runtime. At runtime a template is simply a game object with pre-initialized properties.

A new instance of a template can be created by creating a new game object and assigning the template object to the property Template. The newly created object is now an instance of the template object. At runtime, any number of instances can be created. All instances will have the same properties as the template. (The instances "inherit" the values of the template.)

The template mechanism does not create a one-time copy of the template. Instead, it creates a live relationship between the template and its instances. When the properties of the original template are changed, all instances are affected and automatically get the new template values.

Note Note

Note that changing a property of a template raises property changed notifications on the template object, but does not raise property changed notifications on the instances.

Instances can have local values that differ from the template values. When the game object property of a particular instance is set explicitly, the new value will override the template value. From then on the property of this instance will have a unique, local value. Changing the same property on the template object will no longer affect the instance. The game object property can be reset by calling Reset. The property of the instance will revert back to the value stored in the template.

Using templates saves memory at runtime because game objects only store the local values that differ from the template value.

Note Note

A template itself can also be an instance of another template. That means, the property GameObjectTemplate of the template object can point to another game object. In this case, the template "inherits" the values of another template.