Game Development Reference
In-Depth Information
size 20
sparkly false
next "red_widget"
}
This sample declares two instances of WidgetClass , and initializes those instances
with a name, color value, size, and a flag indicating whether the widget in question
is sparkly or not. Each of these settings is called an attribute, and they can be of any
type we desire—string, integer, floating point, boolean, or an array of values (the
color attribute provides an example of this).
Hopefully, you are looking at this and thinking how exactly this format can be
magically loaded and instanced by the Marmalade text parser, since it obviously
knows nothing about WidgetClass . A good question! The answer is that any class
that you wish to parse from an ITX file must first be derived from the Marmalade
class CIwManaged .
The CIwManaged class
The CIwManaged class is the base class used throughout the Marmalade SDK and
by our own classes whenever we want to be able to create instances of them by
loading from a file.
The class provides some virtual methods that we can override to allow the parser
to recognize our own custom classes, and also to serialize them into a binary format
and resolve any references to other classes or resources. It also provides the coding
glue required to instantiate copies of our class at runtime.
This facility is really useful for us as it allows us to make our code more data-driven.
Say we have a class that describes an item that the player can collect. We might have
lots of different item types in our game, so rather than creating instances of them
all in the source code, which only a programmer can then change, we could instead
instantiate them from an ITX file, which a game designer with no coding knowledge
can then edit.
Instantiating a class with the class factory
The first thing CIwTextParserITX will encounter in the ITX file is the class name,
which it will use to create a brand new instance of our class. It achieves this by
using the class factory, which is another part of the IwUtil API.
A class factory is a programming pattern that allows us to generate new instances of
objects at runtime by asking another class (the so-called factory) to create a relevant
class instance for us.
 
Search WWH ::




Custom Search