Added an extensible interaction mechanism between the C++ and Android parts #2538
base: master
Are you sure you want to change the base?
Conversation
… library and the Android part of the application. It includes the ability to send commands with parameters from the C++ part from game's SDL thread, which are processed by methods in the main UI thread of the main activity. Android part can send notifications from UI thread with the result of processing commands or events that have occurred in app to the main SDL thread of the game, in which it is sent to subscribers via SendEvent from Input subsystem. Added binding to AngelScript. In launcher MainActivity, added methods are command handlers: show a toast, open url, share text, request some system info and battery status. Added example of an AngelScript using this functionality.
Source/Urho3D/Android/Android.cpp
Outdated
URHO3D_LOGERROR("No mEnv in PostCommandToAndroid"); | ||
return; | ||
} | ||
jclass mainClass = mEnv->FindClass("com/github/urho3d/UrhoActivity"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be cached globally or variables could be static. Classes are not changing therefore there is no point in doing reflection lookup every time command is posted.
I suppose this is nice. Java's native methods worked fine last time i needed to do this, but such method eliminates need for user to deal with jni which is a plus. What i do not particularly like is "android" part. This functionality could be platform-agnostic. We could call it |
For starters, I don’t use IOS, so I don’t know how everything works there, how to develop programs for it, I don’t have the opportunity to test it. In general, the mechanism was developed by me mainly for use in scripts. If you make a game in C++, then organizing interaction with the platform is not so difficult for you. I am making Android games using Urho3DPlayer + scripts in AngelScript. And so that it was possible to get from scripts access to different functionality that is in the android part (AdMob, IAP, some sys info, battery and so on), without getting into the C ++ part every time - this mechanism was created. With it, you can do game logic in scripts, and platform things in java (at least for android, I don’t know how in iOS it is done). |
It is ok if iOS part is missing. All i am saying is that we should consider other platforms.
Yep it makes sense. This would be a useful addition. 👍 |
It looks pretty useful! |
The JSON format was chosen as a universal format for passing parameters to the called method, for a uniform call to an unlimited set of methods. When you need new functionality on the java side, you simply create a “void Name (JSONObject params)” method in your class that extends the MainActivity and you can immediately call it from a script or C++ by setting the attribute "method" to “Name” in the object and putting it there the rest of the parameters. |
This looks very useful, although I have to say that there is too much sample code in the Android files just for demo purposes. I would say that there should be 2 methods max which would show the 2 way communication - platform-to-engine and engine-to-platform. |
Marking this stale since there has been no activity for 30 days. |
Added an extensible interaction mechanism between the C++ part of the library and the
Android part of the application. It includes the ability to send commands with
parameters from the C++ part from game's SDL thread, which are processed by methods in
the main UI thread of the main activity.
Android part can send notifications from UI thread with the result of processing commands
or events that have occurred in app to the main SDL thread of the game, in which it is
sent to subscribers via SendEvent from Input subsystem.
Added binding to AngelScript.
In launcher MainActivity, added methods are command handlers: show a toast, open url,
share text, request some system info and battery status.
Added example of an AngelScript using this functionality.