Jump to: navigation, search


Many of the programming languages with which you might be familiar implement special member functions to handle potentially complex process like that of construction/initialization and the destruction of objects. Two of the common special member functions are Constructors and Destructors.

HeroEngine's Data Object Model is very different from your average programming environment. Rather than defining your class structure inline with the code, the definition of classes (member fields, enums, etc) is handled by a schema in what we call the DOM. This division between definition and code supports HeroEngine's dynamic (at runtime) Game Object Model or what is commonly referred to as the GOM.

Currently, HeroEngine does not inherently support the concept of constructors/destructors for classes defined by the DOM. However, it is quite possible to implement that kind of functionality in a manner that suits your particular game, using the capabilities provided by HSL.

What problem does this solve?

What problem does this not solve?


HeroEngine Does Not Inherently Support Constructor/Destructors for Classes Defined By the DOM

While HeroEngine has no default support/implementation enabling Constructors/Destructors, it is quite feasible for you to implement such functionality in your HSL systems to take advantage of the benefits these special member functions normally provide.


Initialization of member fields

CautionSign.gif This section details possible implementations you might create, it does NOT necessarily reflect any actual implementations.

There are a variety of different methods you might use to initialize member fields to some default value during instantiation. Since HeroEngine does not inherently support Constructors, you may choose to use one or more of techniques detailed below to initialize member fields to default values.

Create the instance From a prototype

Prototypes are a type of node template. HeroEngine supports instantiation of nodes using a specified prototype as the template for that node via the external function CreateNodeFromPrototype(). Internally, when an instance is created from a prototype, the instance is instantiated using the same base class as the prototype and then any classes GLOMmed onto the prototype are GLOMmed onto the new instance. Having established a mirror of the class structure of the prototype, now all member fields are copied from the prototype to the new instance.

// assume that we have created a prototype in the DOM named "FooPrototype"
var Foo = CreateNodeFromPrototype( "FooPrototype" )

Factory Method Pattern

The [Factory Method Pattern is often generically used to describe a method whose primary purpose is the creation or instantiation of objects. Assuming some implementation of the Factory Method Pattern in HSL is in charge of creating Foo objects, that same HSL code is capable of performing calls to shared functions or implementing some arbitrary initialization for Foos.

Object-oriented example

Assume for the following examples that you have defined two classes in the DOM; Foo and FooFactory


// In the FooFactoryClassMethods script
method factoryFoo() as noderef of class Foo
  foo as noderef of class Foo = CreateNodeFromClass( "Foo" )
  // Initialize the default values
  foo.someStringField = "Bar"
  return foo

Procedural and object-oriented example

HSL supports mixing procedural and object-oriented programming techniques freely. You might decide for your game that the code to "factory" an instance always lives in a public function in the class methods script for the class. This allows you to call this known function to create instances of class X.

// In the FooClassMethods Script
public function construct() as noderef
  var foo = CreateNodeFromClass( "Foo" )
  return foo
method initializeFoo()
  me.someStringField = "Bar"

The Spec System

Main page: Spec System

The MMO Foundation Framework includes an extremely sophisticated implementation of the Factory Method Pattern called the Spec System. Each spec implements the method CreateFromSpec() which factories up a particular instance based on the construction of the spec. During the instantiation of what is called a "SpecDerivedObject", calls are made to the classes from which a spec is comprised, allowing them to perform any kind of initialization/construction behaviors which might be required.

Personal tools