Game Development Reference
In-Depth Information
You now have enough code to add objects to a scripted scene and update them. You also are halfway into sending
the updated objects back to the Java GUI. But in its essence, scene playback is already possible. There's only one thing
missing. What if the user, after watching a playback of the scene, decides to go back to the scene editor and delete an
object? What if they want to add objects or edit the script of an existing object? The script system needs a way to reset
itself to allow initialization of a new or modified scene. The
Reset
function removes all the script modules from the
engine, as shown in Listing 12-19.
Listing 12-19.
The Reset Function
void GPTScriptEngine::Reset()
{
// Remove all modules from script engine
for (size_t i = 0; i < m_scriptModules.size(); ++i)
{
std::stringstream moduleStream;
moduleStream << m_scriptModules[i].id;
std::string modName = moduleStream.str();
m_pScriptEngine->DiscardModule(modName.c_str());
}
// Clear modules
m_scriptModules.clear();
}
The code that glues the script engine and the Java GUI together is in
GPTMain.cpp
. It contains the two functions
declared in
GPTJNILib.java
:
Initialize
and
Update
as well as a declaration for the global variable
gScriptEngine
.
Due to JNI naming conventions, function signatures are longer than you may be used to. They start with “Java” and then
include the package name, followed by the class name, ending with the function name, all separated by underscores.
The
Initialize
function takes a container of game objects, as sent by the scene player. It resets the script
system to start from scratch and proceeds to add each object. Because game objects are sent in the form of an
ArrayList
of
GPTGameObject
Java instances, special JNI functions are used to get individual instances and their
property values. For example, to get the color of the first object in the
gameObjects
container, you would write the
code shown in Listing 12-20.
Listing 12-20.
How to Get a Property Value from a Java Object in C/C++
jclass editorGameObjClass =
env->FindClass("com/gametoolgems/gpt/GPTEditorGameObject");
jfieldID gameObjColorField =
env->GetFieldID(editorGameObjClass, "color", "I");
jobject gameObj =
(jobject)env->GetObjectArrayElement(gameObjects, 0);
jint objColor = env->GetIntField(gameObj, gameObjColorField);
First, you get a handle on the Java code's
GPTEditorGameObject
class. The fully qualified name is required, which
also includes the package name. Second, you get a handle on the
color
property of the class by calling the
GetFieldID
function with the class handle and the name of the property. The last parameter in the
GetFieldID
function is a way
to tell it what the data type is. The “I” means it's an integer type. Next, you use the
GetObjectArrayElement
function
and send it an index of 0 to get the first object in the array list. And last, the color value is obtained by calling the
GetIntField
function and passing it the object and the handle to the
color
property. The full code for the
Initialize
function is shown in Listing 12-21.