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
115 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:

Introduction to GameMonkey Script Part 1
Language Introduction


Expression and Conditional Syntax

GameMonkey Script can evaluate common expressions using the standard C-style expression syntax and as such, anyone familiar with C or C++ will feel comfortable using GameMonkey Script expressions.

x = 5;			// Assign a value to x
print( x );

y = (x * 10) + 56;	// Assign a value to y
print( y );

if (x == 10)		// Comparison (equality)
{
	print( "x is equal to 10" );
}
if (x != 10)		// Comparison (inequality)
{
	print( "x is not equal to 10" );
}
if (x < 10)			// Comparison (less than)
{
	print( "x is less than 10" );
}
if (x > 10)			// Comparison (greater than)
{
	print( "x is greater than 10" );
}

Example code: expressions_1.gm

GMScript also allows bitwise operations such as or ('|'), xor ('^'), and ('&') and bitwise complement ('~'). Logical operators include or ('||'), and ('&&') and logical complement ('!'). GMScript also allows the use of the keywords and and or to be used in place of && and || respectively.

One can employ conditional logic in GameMonkey script by using the if statement.

if ( <condition expression> )
{
	// Do something if true
}
else if ( <second condition> )
{
	// Do if 2nd condition passes
}
else
{
	// Do if all are false
}

Unlike C/C++, GM Script does not contain a switch statement so one must emulate the functionality by using blocks of if / else if tests. Also unlike C/C++, the body of conditional statements such as if must be specified as block statements surrounded by curly braces, to do otherwise is an illegal syntax.

Loops and iterations

GameMonkey script has several methods of executing a loop. The first is the familiar for statement:

for (<statement>; <condition>; <statement>)
{
	// repeated statement
}

A simple example to iterate a variable and print the number it contains would be:

for (it = 0; it <= 10; it = it + 1)
{
	print( "it = ", it );
}

Example code: loops_1.gm

The output will be the numbers from 0 to 10 printed on separate lines in the output console.

Like C/C++, for loops may contain empty statements (effectively simulating a while loop). However like the if statement, the body of the for loop must be surrounded by curly braces. For example:

for (; it != 10;)
{
	print( "it = ", it );
	it = it + 1;
}

The while statement is used in situations where the conditions around the loop aren't as certain; the most common use of the while loop is to loop until a particular flag is set. Again, like the for statement the body of the while statement needs to be enclosed in curly braces.

while ( <condition> )
{
	// repeated statement
}

For example, to repeat until the user has pressed the 'quit' button:-

while ( !quitButtonPressed )
{
	// do something in the game
	quitButtonPressed = host_check_for_quit();		
}

Note that the host_check_for_quit function is a hypothetical application-bound function. Similarly, an approximation of the for loop you saw previously would be:

it = 0;

while (it <= 10)
{
	print( "it = ", it );
	it = it + 1;
}

Example code: loops_2.gm

The foreach loop allows you to iterate over the contents and keys of a table. I will cover this in more detail in the table section of this article.

That was a brief overview of the various statements, expressions and symbols used in the GM Script language; however it is far from exhaustive. For a full list of expressions and iterators along with their syntax you are advised to consult the GameMonkeyScriptReference which comes with the official GameMonkey Script releases. It is worth noting that the GMScript reference does contain a few errors; notably it claims that ~= is a valid assignment operation, which is not the case as it fails both to parse and make sense!

Scripted Functions

The GameMonkey Script machine has two forms of functions. The first is the scripted function, a function that is created and coded in script. A scripted function is specified in the same way as a normal variable:

myMultiply = function( x, y )	{ return x * y; };

As functions are created and specified as variables, it is extremely important to remember the terminating semi-colon at the end of the function declaration. Calling a function is as you'd expect from a C-style syntax:

a = myMultiply( 100, 2 );
print( a );

Example code: functions_1.gm

The second type of function is that of the host-declared function. An example of a native function is the print command which is contained within gmMachineLib and bound to every gmMachine you create. Only C-style functions or static class methods can be bound to the GameMonkey machine, but there are workarounds to this. I will cover the binding of native functions in the second part of this article.





The Table Type


Contents
  Introduction
  Introduction to the Language
  Expression and Conditional Syntax
  The Table Type
  Simulation with Tables
  Further Exploration

  Source code
  Printable version
  Discuss this article

The Series
  Language Introduction
  Embedding GameMonkey