This article and all its contents are © 2000 David Goodlad. They may not be reproduced in any form without permission from the author.
I will very gladly give permission to copy the article, though, so send me an email!
Welcome to the third part of this series of articles about what should be recognized as one of the most important pieces of game programming: the addition of a simple scripting lanugage. In this part the focus will be on the implementation of control structures into your language. They will all be coded as regular commands, written in the same way as you would implement any other custom commands for your language. A reminder that, as usual, the actual implementation of all of these things was done to maintain the ability to be understood by you people easily. Therefore, I admittedly have made the functions somewhat 'klunky' and not necessarily the best they can be. A bigger explanation of what can be done about this is in the conclusion... But, remember, these examples are designed to be learned from, not copied, because they will not work very well in practical uses. But, anyways, enough chatter, let's get on with it!!
The first requirement
Before we can actually start making 'decisions' in the language, there must be some way for a script to set and/or retrieve variables: in this case, bit flags. These have been chosen for simplicity; they are simple boolean values stored in a 'compressed' format, so that you can have lots of them. You would probably want named 'variables' in your actual game, but to keep things easy to understand, I'll stick with bit flags for now.
Bit flags are simple to implement. The actual reasons for it working are out of the scope of this article, so I won't bother explaining really. But, here are three functions, one which sets a flag, one which unsets a flag, and one which returns the value of a flag, as well as the definition for the 'Flags' array. As usual, these should be put in your CScriptParser class.
If it's not self-evident to you, there are 8 bits per 'Flags' array item, numbered 0 to 7. To call these functions from your script, simply use the format below:
So, for example, you wanted to set bit number 5 of flag 17. You would call:
Not hard, huh? Now that we've got that down, time to implement a simple If statement!