Event notification possible from java module to gd script ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kaushik
:warning: Old Version Published before Godot 3 was released.

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 ?

:bust_in_silhouette: Reply From: Zylann

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).

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++.

Reyalpsirc | 2017-01-02 23:14

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 :confused:

Zylann | 2017-01-03 00:28

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.

Reyalpsirc | 2017-01-03 17:51

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 :slight_smile:

Shin-NiL | 2017-01-04 16:20

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.

Reyalpsirc | 2017-01-04 16:30

Nice, thanks for sharing :slight_smile:

Shin-NiL | 2017-01-04 16:48

:bust_in_silhouette: Reply From: Kevin

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});


I hope it can help you.

This works fine. Thanks!

Kaushik | 2016-12-29 06:48