godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.05k stars 65 forks source link

Add support for calling GDScript methods from Android plugins #9638

Open theromis opened 2 weeks ago

theromis commented 2 weeks ago

Describe the project you are working on

I'm working on FirebaseMessaging plugin

Describe the problem or limitation you are having in your project

I understood that I can't call register FCM token from plugin, at least can't find proper example

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Want to be able to register callable and use it from inside plugin

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

func register_fcm_token(token: String) -> void:
    rest_request_for_fcm_token_reg(token)

gn = Engine.get_singleton("GodotNotification")
if gn:
    gn.register_fcm_callable(register_fcm_token)

inside plugin Android Java plugin should be

...
import com.google.firebase.messaging.FirebaseMessagingService
...
class GodotNotification(godot: Godot) : GodotPlugin(godot), FirebaseMessagingService() {
    private val fcm_callable: Callable = null
    @UsedByGodot
    fun register_fcm_callable(callable: Callable) {
       fcm_callable = callable
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        sendRegistrationToServer(token)
    }

    private fun sendRegistrationToServer(token: String) {
        if (fcm_callable != null) {
            fcm_callable.Call(token)
        }
    }
}

If this enhancement will not be used often, can it be worked around with a few lines of script?

Hopefully it can

Is there a reason why this should be core and not an add-on in the asset library?

It should be useful for any modules with reverse callable options

theromis commented 2 weeks ago

@Calinou thank you for corrections but I think same logic needs for ios/macos modules/plugins

Calinou commented 2 weeks ago

@Calinou thank you for corrections but I think same logic needs for ios/macos modules/plugins

Considering an implementation will likely only target one platform at first, I suggest opening different proposals to track iOS and macOS support respectively.

tokengamedev commented 1 week ago

I am not getting the exact need and why it is required. This approach tightly couples between plugin and the main application. It is unwarranted as plugins were supposed to be loosely coupled. Signal would be the right approach for the example given above as plugin is not aware of the state of the application.

I am asking this to understand, I am not criticizing the proposal. I am certain there will be a use case to tightly bind main application with plugin, but yet to understand the use case for Godot game engine plugins with android.

theromis commented 1 week ago

I thought about signal, just thought I can't emit it from plugin. Is this signal plugin based (declared in plugin) or opposite it defined in the code (in my case GDScript) and plugin can emit it if it declared?