For certain C++ classes it would be very useful if they could be exposed to the scripting language, so they can be constructed and managed by scripted classes. To establish this link to C++ the language has the native keyword. This keyword can be used in a couple of different locations, which will be discussed below.
In order to use C++ classes in scripts, you first have to expose these classes to the virtual machine.
Each native class can be marked as such by placing the native keyword in front of the class declaration.
:::Java
native class Entity
{
public void method() {
}
}
There are some rules for the use of native here:
Native constructors
Functions can be marked native in two ways. A function is either pure native or native. Pure native functions look like abstract functions. You mark such a function with the native keyword and give it no body.
:::Java
native class Entity
{
public native void pureNativeMethod();
}
Native: you optionally supply the native keyword at the function declaration. In the body of the method you can the native function, by using the native keyword again, optionally with arguments.
:::Java
native class Entity
{
public native int nativeMethod() {
int result = native();
return result;
}
}
In the example above the function nativeMethod is defined using all optional parts: