m4gr3d / Godot-Android-Samples

Collection of Godot Android plugins
MIT License
43 stars 6 forks source link

Unable to send data from native android to Godot via signals #9

Closed kyadalu1 closed 1 year ago

kyadalu1 commented 1 year ago

I am trying to integrate signals via android plugin. Below is my android code

class GodotAndroidPlugin(godot: Godot) : GodotPlugin(godot) {

    override fun getPluginName() = "MyPlugin"

    override fun getPluginSignals(): MutableSet<SignalInfo> {
        val signals: MutableSet<SignalInfo> = mutableSetOf();
        signals.add(SignalInfo("testSignal", String::class.java))
        return signals
    }

    @UsedByGodot
    private fun helloWorldSignal(name: String) {
        runOnUiThread {
            Toast.makeText(activity, "Hello $name", Toast.LENGTH_LONG).show()
            emitSignal("testSignal", "Hello $name")
        }
    }

}

When i call the helloWorldSignal method from Godot side i am able to see the Toast but i am not able to get the data which i am sending using the emitSignal method

My Godot code

var _plugin_name = "MyPlugin"
var _android_plugin

func _ready():
    if Engine.has_singleton(_plugin_name):
        _android_plugin = Engine.get_singleton(_plugin_name)
        _android_plugin.connect("testSignal",self,"on_testSignal")
    else:
        printerr("Couldn't find plugin " + _plugin_name)

func _on_button_2_pressed() -> void:
    if _android_plugin:
        _android_plugin.helloWorldSignal("Shivaji")

func on_testSignal(data):
    label.text = data
    print(data)

on_testSignal is never called.

I tried looking into the signal repo which you have created but you're not sending any data from native android to godot side. Sorry i am pretty new to Godot

m4gr3d commented 1 year ago

@kyadalu1 The syntax you're using to register the signal is incorrect; you can either use

_android_plugin.connect("testSignal", Callable(self,"on_testSignal"))

or

_android_plugin.connect("testSignal", on_testSignal)

I tried looking into the signal repo which you have created but you're not sending any data from native android to godot side.

The gltf viewer sample sends a signal with a String data, you can use it for reference.

kyadalu1 commented 1 year ago

Thank you