| |
|
|
||||
![]() |
||||||
| |
|
|||||
|
Listing 1. Native code making use of Java.
// Java class, server-side Game Logic scripting. package somegame; class ScriptEngine { // This uses native error() callback. public static void init() {..} ... public static native void error( int code ); } #include <jni.h> // Native error callback provided to Java. extern void SV_ErrorScriptEngine( jint i ); JNINativeMethod svSE = {"Java_somegame_ScriptEngine_error","(I)V", SV_ErrorScriptEngine }; // Native function to set up scripting module. void SV_InitScriptEngine( JNIEnv* env ) { jclass clazz = NULL; jmethodID method = NULL; jint err; // Lookup class, loads the class if not yet done. clazz = (*env)->FindClass( env, "somegame.ScriptEngine" ); if ( clazz != NULL ) { // Register native method, returns zero on success. err = RegisterNatives( env, clazz, &svSE, 1 ); // Lookup Java methodID. method = (*env)->GetStaticMethodID(env, clazz, "init", "()V"); } // Handle errors. ... // Call static method. (*env)->CallStaticVoidMethod(env,clazz,method); } It is tempting to use static (class) methods, as you do not have to handle an object in addition to the class. In many cases this is absolutely sufficient — servers will likely not run two script engines in parallel. In many other cases though, this leads to bad object-oriented design on the Java side. |
|
|