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 2
Embedding GameMonkey


Executing a String as a Script

An embedded virtual machine is next to useless without a script to run on it, so this next section will cover the most basic way you can execute a script on the GameMonkey VM. The gmMachine object you created earlier has a member function called ExecuteString which provides you with a method to execute a text string as a script on the GM Machine. Let's see a simple example of this in action:

#include "gmThread.h"

int main()
{
	gmMachine gm;

	// Execute a simple script
	gm.ExecuteString( "print( \"Hello, world!\" );" );

	return 0;
}

Example: basic_2.cpp

Compiling and running this program should display the immortal words "Hello, world!" in your console window.

Here, because the text of the script contains quotation marks, we need to escape the quotations in the C++ string. However, GMScript also allows backquoted strings, like so:

gm.ExecuteString( "print( `Hello, world!` );" );

Backquotes are not special characters in C++ strings so they do not need to be escaped. Also, inside the backquotes, GMScript does not process escape sequences either, though it does process a double backquote as a single backquote to be embedded in the string.

`fred``s` is equivalent to "fred`s"

It should be noted that a backquote in GMScript is not a single quote (') and so they cannot be substituted for each other.

Providing GameMonkey Script with a script string containing double-quoted string literals poses us a problem as we're using C++ and we will need to escape these elements in order to compile our program.

In the following code snippet, I store my script in a const char* string and pass the data to GM for execution:

#include "gmThread.h"

int main()
{
const char *myScript = "fruits = table ( \"apple\", \"pear\", \"orange\" ); "
                       "foreach ( frt in fruits ) { print(frt);	} ";

	gmMachine gm;

	// Execute a simple script
	gm.ExecuteString( myScript );

	return 0;
}

Example: basic_3.cpp

You could utilise GMScript's backquoted strings to make this script more manageable:

const char *myScript = "fruits = table ( `apple`, `pear`, `orange` ); "
                       "foreach ( frt in fruits ) { print(frt);	} ";

Similarly, a seemingly simple script command can become complicated without the use of backquotes when stored in a C++ string:

path = "C:\Windows\System";

const char *myScript = "path = \"C:\\\\Windows\\\\System\";";

Each of the double quotes needs to be escaped inside the C++ string. Then each of the of \'s needs to escaped, once since they're inside a C++ string, and then each of those needs to be escaped since they're inside a GMScript string.

Again the use of backquoted strings makes this slightly more manageable:

const char *myScript = "path = `C:\\Windows\\System`;";

Now only the \'s need to be escaped since they're inside a C++ string, but they don't need to be escaped again since they're inside a GMScript backquoted string.

As you can see, hard coding scripts as C++ strings can quickly become messy as you need to remember to correctly escape each special character in the context it is used. Furthermore, it actually defeats the need for a scripting language in the first place; your strings are still compiled along with the application and any changes to them forces a recompile!





Executing a Script from File


Contents
  Basic Embedding Concepts
  Executing a String as a Script
  Executing a Script from File
  More on Script Execution
  gmVariable Object
  Calling a Scripted Function
  Creating a host-bound function
  Creating a simple type
  Constructor with Parameters
  Operator Overrides
  SetDot Operator
  Garbage Collection

  Source code
  Printable version
  Discuss this article

The Series
  Language Introduction
  Embedding GameMonkey