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


Introduction to the Language

In the following section I will introduce you to the GameMonkey Script language. You will learn how to manipulate variables, how to create and call functions and how to use the powerful GM Script table type. This section is intended to be a primer to the language and it is assumed that you have some programming knowledge of C, C++, Java or other similar syntax styles. You can follow the simple examples I present here and experiment yourself using the gme executable that comes with standard GameMonkey Script distributions. gme is a stand-alone interpreter for GameMonkey Script which provides a basic GameMonkey Script environment for you to load your scripts into and test them. Because of this, gme will be a useful tool for your first steps into learning the GMScript language and syntax.

Using the gme interpreter is simple; from the command line you type 'gme path_to_script_file' (ensuring that gme is in your current directory). The script will then load and be executed, showing you the results in the console window.

Syntax Overview

This following section will give you an overview of the syntax used by the GameMonkey Script language. You will notice from the provided examples that the basic syntax of GM Script is very much like that of C; because of this GMScript may not be as instantly accessible as Lua or Python which aim to be simple for non-programmers to pick up.

GM Script features the common elements of most programming languages:

  1. Variables
  2. Comments
  3. Expressions
  4. Selection Statements (if / else)
  5. Loops (for, while, foreach)
  6. Functions

GameMonkey Script also features a built-in cooperative threading and state system, allowing you to create multiple threads within the virtual machine to run several parallel tasks at once. However, I will not venture into that territory in this introductory article.

GameMonkey Scripts: Defined

A script in GameMonkey is usually a plain-text ASCII file that contains a collection of functions, variable declarations and expressions. Scripts are usually loaded in from disk and compiled into a form that the GM environment can work with, namely bytecode. There is no standard form to a GameMonkey Script; you do not have to import modules like in Python, nor do you have to adhere to any indentation formatting (again, like Python). GameMonkey script does not have a native version of the include directive, so usually a script is self-contained (although you can load and execute multiple scripts on the same machine). The only requirement in GameMonkey Script is that function variables are declared before you try and call them; the ordering of such statements is important because GMScript code is compiled in a single-pass and also it does not have a pre-processor like in C/C++.

GameMonkey Variables

Unlike C/C++, GM Script is a dynamically typed language; variables can assume any type at any time during the execution of a script. Also, variables do not need to be declared before use; one can simply make the variable assignment and the variable will be created and be of the type relevant to the data it holds. The basic types of GameMonkey Script are integers, floats, strings, functions, tables and null. Here is an example of some simple variables in use:

myNull = null; // has type null, which is a distinct type
myNum = 50;	// has type of int
myString = "Hello!";	// has type of string
myFloat = 3.14;	// has type of float

print( myNum );
print( myString );
print( myFloat );

As you will see, a variable declaration is as simple as assigning a value; variables that haven't been assigned will have the value and type of null. You will notice that every line is terminated by a semi-colon. This denotes the end of a statement and unlike Lua, is non-optional in GameMonkey Script. To C/C++ developers this will come naturally and so shouldn't cause too many problems.

null is a distinct and important type in GMScript; a variable of null type is said to have no value. When referencing variables for the first time they automatically have the type null and will remain that way until an assignment is made. You should be careful when using nulls in comparisons and expressions; for example, concatenating a string with a null variable will result in the text 'null' being appended to the string. However in integer expressions, a null variable is interpreted as being zero. This behaviour is different to some languages, such a T-SQL where operations involving null variables will themselves yield a null result. It should also be noted that assigning an existing variable be of type null will allow it to be garbage collected by the GMScript machine; this consideration is especially important once you begin to create and bind your own types from C++ to the machine, but this shall be covered in the next article.

Comments

Comments in GM Script are exactly like those in C++; you can choose between C++ style line-comments or C-style block comments:

x = 5;		// This is a line comment

/*
This is a block comment
*/

Like C++, block comments cannot be nested in GMScript. Because of this you can run into trouble if you are using the block comment features of the language to comment out sections of code. It is worth considering this when writing code as there is currently no GMScript-aware IDE that is intelligent enough detect this.





Expression and Conditional Syntax


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