Game Development Reference
In-Depth Information
#endif // _GPT_SCRIPT_ENGINE_H_
The ScriptModule structure maps a game object to an AngelScript module. Your basic implementation uses one
module per game object, which is a very simple way to integrate scripting, however it imposes a big overhead for the
application. A better way would be to use script classes, which are a standard part of AngelScript. For your purposes,
one module for each game object is good enough.
Having a vector of ScriptModule instances helps iterate through all the game objects and call special script
functions on them, such as Update . The GameObject structure is a helper that allows the script engine to provide an
updated vector of objects via the GetUpdatedObjects function. The other important members are the AngelScript
engine and context. Only one context is necessary for simple scripting.
Now that the blueprint is finished, let's begin with the GPTScriptEngine.cpp implementation file. You will define
some common script code that will be added to all of your AngelScript modules. First, you need a scripted representation
of a game object to be able to modify properties like position and color. Second, a global game object is created. Since
this code will be added to every module, each instance will be global only to its unique module. The reason to make it
global is to allow access to game object properties via a named reference (i.e., me.color ). Finally, a set of global functions
is defined. PreInit is called automatically when adding a game object to the system and it copies the property values to
the scripted representation. GetProperties is called in a post-update step to gather the values of each game object and
pass them along to the GUI. Listing 12-14 shows C/C++ code that defines strings with AngelScript code inside.
Listing 12-14. Common Script Code for All Modules
const char* classesCode = \
" \n" \
"class CGameObject \n" \
"{ \n" \
" public int id; \n" \
" public float x; \n" \
" public float y; \n" \
" public int color; \n" \
"} \n" \
" \n" \
;
const char* globalObjectsCode = \
"CGameObject me; \n" \
;
const char* globalFunctionsCode = \
" \n" \
"void PreInit(float x, float y, int color) \n" \
"{ \n" \
" me.id = id; \n" \
" me.x = x; \n" \
" me.y = y; \n" \
" me.color = color; \n" \
"} \n" \
" \n" \
"void GetProperties(float&out x,float&out y,int&out color) \n" \
"{ \n" \
" id = me.x; \n" \
" x = me.x; \n" \
 
Search WWH ::




Custom Search