Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signals and reported nonexistent signals #475

Closed
lorenalexm opened this issue May 21, 2024 · 2 comments
Closed

Signals and reported nonexistent signals #475

lorenalexm opened this issue May 21, 2024 · 2 comments

Comments

@lorenalexm
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants