+4 votes

Suppose I have written a custom java module, and I am monitoring a variable which is used in various places in the module and should hold intended value in onMainActivityResult() block. From my gd script, I can perform polling in a continuous loop (through getter methods defined in the java module) and monitor the variable value. However, since I know the variable value will be useful only during the activity result block is being executed, the polling mechanism is a useless overhead.

Is there any way an event notification can be performed from a java module to the calling gd script ?

asked Dec 29, 2016 in Engine by Kaushik (51 points)

2 Answers

+2 votes
Best answer

These codes come from Godot Docs

From Java, use the calldeferred function to communicate back with Godot. Java will most likely run in a separate thread, so calls are deferred:

GodotLib.calldeferred(<instanceid>, "<function>", new Object[]{param1,param2,etc});

http://docs.godotengine.org/en/stable/reference/creating_android_modules.html#java-singleton

I hope it can help you.

answered Dec 29, 2016 by Kevin (82 points)
selected Dec 29, 2016 by Kaushik

This works fine. Thanks!

+1 vote

You should be able to emit a signal from your C++ class, just call emit_signal("stuff_happened", args...);. If a script is connected to that signal then it will be called.
However check when this execution block happens. Is it from another thread? If yes, I'm not sure if signals are thread-safe, maybe they are, there is probably a way to make them deferred (since the engine does that for networking and threads).

answered Dec 29, 2016 by Zylann (26,129 points)

Is there a way to call emit_signal from the Java singleton class?
If not, how can I mix the Java class with a c++ class? I know Java but I'm still new to c++.

emit_signal is to be emitted from the C++ class at some point (so scripts can listen to it), however I have no experience in calling the C++ class from Java :/

Yeah, I managed to get emit_signal to work with ADD_SIGNAL for the iOS part of my module (so I can use connect on the gdscript side). The problem is how can I make the same functionality work for the Android part knowing that I have a java dependency lib.

I'm interested in this kind of solution too. I would like to develop a module for Android and iOS with a unified way of accessing GDScript code. The use of signals would be the most elegant way.

Please share your progress with us :)

Well, I ended up by using the calldeferred on the Android side and then do something like what is done for the IAP's of Android. Still, I kept the emit_signal on the iOS side.
The only downside is that this makes me have an extra GDScript for communication between the actual code on the App side and the module itself.

Nice, thanks for sharing :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.