Integrating an Existing Language

When it comes to using a scripting language, the decision has to be made as to which parts of the game should be written in the script language and how much of the game data should be maintained on the script side. One extreme of the spectrum is to implement all the functionality and manage all significant data on the native side. The other is to keep as much functionality and data on the script side. The former is most common because too much data and code on the script side can have substantial performance implications. This is not the case if the scripting language is Java, for reasons explained in the previous sections. In fact, Vampire: The Masquerade used Java to implement more than just basic scripts. The Nod SDK came with about 500 Java source files. The scripts communicated with objects on the native side as well as objects that were fully maintained on the script side. Chrome is an example of a more recent game that uses Java strictly as its scripting language.

As with the other extreme, choosing to maintain all significant data on the native side is as practical for Java as it would be for any other language. Scripts can manipulate the game objects using native accessors. It is typical for this approach to simply store an object identifier on the script side. When a native function is invoked, the identifier is used to indicate which object the function should manipulate.

The first step to using a scripting language is to be able to launch its VM. Chapter 11, “Java Native Interface” has a section titled, “The Invocation Interface,” which shows how to launch the VM from C/C++ code. The next step is to expose native methods so that they can be accessed from the script side.

 On the CD   The source for a demo that shows how to use Java as a scripting language has been provided on the CD-ROM in code/Chapter 11/Scripting Example. ScriptBase acts as the base class for other script files. The base class allows a script file to have convenient access to the common methods, constants, and variables. A sample script file extends the ScriptBase class and creates several units, as follows:

class Script1 extends ScriptBase{     void run(){                   print("Script1.run()");             Unit peons[] = new Unit[10];              for(int i=0; i<peons.length; i++){             peons[i] = createUnit(UNIT_TYPE_PEON);         }                  for(int i=0; i<peons.length; i++){              destroyUnit(peons[i]);         }     } }

The demo shows how to launch the VM, register the native functions for creating and destroying units, load the compiled version of the script file, and execute it.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net