Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
96 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:


 First Requirement
 If Statements
 More Theory

 Printable version


  The Series

 Doing Something
 Control Structures


Copyright, etc.

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!

David Goodlad
webmaster: http://blackhole.thenexus.bc.ca


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.

Dim Flags(255) As Byte 'Put this in the Definitions section Public Function ExecuteCmdSetFlag(a() As String) As Integer Flags(a(0)) = Flags(a(0)) Or (2 ^ a(1)) 'Set bit number a(1) of flag number a(0) 'a(0) is the first parameter, a(1) is the second ExecuteCmdSetFlag = 1 'Return 1 to indicate success End Function Public Function ExecuteCmdUnSetFlag(a() As String) As Integer Flags(a(0)) = Flags(a(0)) And (255 XOr (2 ^ a(1))) 'Unset bit number a(1) of flag number a(0) 'a(0) is the first parameter, a(1) is the second ExecuteCmdSetFlag = 1 'Return 1 to indicate success End Function Public Function ExecuteCmdGetFlag(a() As String) As Integer Dim i As Integer i = Flags(a(0)) And (2 ^ a(1)) 'Put the value of bit a(1) of flag a(0) into i If i > 0 Then ExecuteCmdGetFlag = 1 Else ExecuteCmdGetFlag = 0 'If the bit was set, return 1, otherwise return 0. End Function

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:

SetFlag (flagnumber,bitnumber) UnSetFlag (flagnumber,bitnumber) GetFlag (flagnumber,bitnumber)

So, for example, you wanted to set bit number 5 of flag 17. You would call:

SetFlag (17,5)

Not hard, huh? Now that we've got that down, time to implement a simple If statement!

Next : If Statements