This way you can write Java code and utilize any Android Java API, and communicate with Pascal.
The CastleMessaging
is a simple asynchronous communication mechanism between Pascal and Java, using it is easy (you have ready methods on both Pascal and Java side, and you don’t need to deal with JNI).
-
On the Pascal side, use CastleMessaging
unit, that exposes a singleton Messaging
.
Typically, you wrap sending and receiving messages in a nice Pascal API. Examples of it are in the CGE src/services/
directory.
-
On the Java side, you create a class descending from ServiceAbstract
. Such class can override messageReceived
method to receive messages, and use messageSend
to send messages. Examples of it are CGE services inside tools/build-tool/data/android/integrated-services/XXX/src/io/castleengine/ServiceXXX.java
files.
See e.g. Google Play Games handling:
-
the Pascal part is in src/services/castlegameservices.pas
,
-
the Java part is in tools/build-tool/data/android/integrated-services/google_play_games/src/io/castleengine/ServiceGooglePlayGames.java
.
The Java code of this particular service is no longer straightforward (it has a lot of functionality by now…), but it shows how to pass messages back and forth.
For something simpler, see the "vibrate" service:
Note that CastleMessaging
is not supposed to be an "API for all communication", but in practice it works very well in all existing services — since it’s natural for asynchronous processing (where many things are done in some thread, and/or wait for network or user input before returning any result).
The exact messages are not documented anywhere, since they are the "internal" API between Pascal and Java code. You should just cross-reference the castlegameservices.pas
and ServiceGooglePlayGames.java
and see that what one sends — the other receives:) When creating new service, you just invent your own messages, using any non-conflicting names.
In the Java part, you use the full power of Java APIs on Android, that are necessary to access various Android stuff. On the Pascal side, you create a thin wrappers that merely send/receive messages. Final games should use the Pascal API, without being aware of any "messages" underneath, e.g. see how castle-engine/examples/2d_dragon_spine_android_game/
uses the TGameService
class.
Note
|
The disadvantage of CastleMessaging approach is that the communication between Java and Pascal looks like an asynchronous network channel. That’s OK in many cases (especially when the Java code indeed wraps some network operations), but it may be bad if you need to get something fast/synchronously. For such cases, using JNI to implement (possibly bidirectional) communication between Java and Pascal is necessary.
|