migueldeicaza / SwiftGodot

New Godot bindings for Swift
https://migueldeicaza.github.io/SwiftGodotDocs/tutorials/swiftgodot-tutorials/
MIT License
985 stars 55 forks source link

Signals and reported nonexistent signals #475

Closed lorenalexm closed 1 month ago

lorenalexm commented 1 month ago

I know this is a bit more of a Godot issue and a bit less of a SwiftGodot one, so I apologize for that. Historically with other engines I have used a bespoke Event/Message broker to inform objects in a decoupled way. In migrating to (Swift)Godot I am trying to utilize its way of doing things.

Signals.

I have two classes (trimmed for clarity) with one declaring the signal, and another attempting to connect to that signal. When running the project, the console reports that the declared signal is a nonexistent one and of course the onCoinCollected function is never called. I have seen in issue #254 that it seems we need to couple our classes to correctly connect to a signal? In this case having the GameInterface class @Export a property to reference the Player.

Is this correct thinking on my part? Are we truly needing to pass references around to connect to a signal?

Thanks!

// GameInterface.swift
import SwiftGodot

@Godot
class GameInterface: CanvasLayer {
    // MARK: - Properties
    @BindNode var Coins: Label

    // MARK: - Functions

    override func _ready() {
        GD.print("Attempting to connect signal")
        connect(signal: Player.coinCollected, to: self, method: "onCoinCollected")
    }

    @Callable func onCoinCollected(total: Int) {
        GD.print("coinCollected signal received.")
        Coins.text = "\(total)"
    }
}
// Player.swift
import SwiftGodot

@Godot
class Player: CharacterBody3D {
    // MARK: - Properties
    var coins = 0

    #signal("coinCollected", arguments: ["count": Int.self])

    // MARK: - Functions

    func collectCoin() {
        GD.print("Coin collected!")
        coins += 1
        emit(signal: Player.coinCollected, coins)
    }
}
migueldeicaza commented 1 month ago

Would you mind asking questions on the Discussion page, rather than the issues page?

I am closing here, and if we find a bug, we can move that bug here.

lorenalexm commented 1 month ago

Oh crap! Completely forgot there was even a discussion section; my apologies!