Game Development Reference
In-Depth Information
With the preceding classes, it would then be possible to calculate the result of the
AST by calling
Execute
on the root node. Once that call returns, the lone value
on the stack will correspond to the result of the operation.
For example, the expression
5 + 6
would have an AST where the root node is
an
Addition
class, the left child is an
Integer
class with a value of
5
, and the
rightchildisan
Integer
classwithavalueof
6
.Soif
Execute
iscalledonthat
root
Addition
node, it would first call
Execute
on
5
, which will push
5
onto
the calculator stack. Then
Execute
on
6
pushes
6
onto the calculator stack. Fin-
ally,
Execute
on the root mode will add the top two values on the stack, which
gives
11
, and push that back on, giving the final result of the operation.
Of course, if the scripting language in question supports far more than just basic
arithmetic, the contents of the
Execute
functions will become more complex.
Butthisbasicprinciple ofapost-ordertraversal carries overregardless ofthecom-
plexity of the language or node type.
Data Formats
Another decision that must be made during game development is how the data de-
scribing elements such as levels and other game properties should be stored. For
a very simple game, you might be able to get away with having most of the data
hard-coded, but it's not an ideal solution. By having data stored in external files,
you allow for nonprogrammers to edit it. This also opens up the possibility of cre-
ating tool programs (such as a level editor) that allow the data to be more easily
manipulated.
When you're deciding on a data format, the first decision is whether or not the
data should be binary or text-based. A
binary
file is one that contains data that's
mostly unreadable by a human. If you open up a binary file in a text editor, it will
be a stream of random characters. An example of a binary format is PNG or any
other image file format. A
text-based
file, on the other hand, is a format that is
composed of standard ASCII characters and therefore can be read by a human. As
with the decision on whether or not a scripting language should be used, there are
tradeoffs between both approaches. Ultimately, it will make sense to store some
data in a binary format while storing other data in a text-based one.