• Godot Help
  • Questions about application and/or intended usage of signals.

In my titlescreen I have 4 saveDoors, each corresponding to a save. Each door's on_body_entered signal is connected individually to the level. (disconnected from LevelManager, and reconnected to titlescreen.gd), and the appropriate save is loaded when the player interacts. (The way interacting currently works is that the objects that can be interacted with are on a different collision layer. When the player presses the action, they are temporarily moved to the layer the door is on. When the action is released, the bit is switched back off).



Should my current implementation be changed? As I have it now, each doors on_interact is connected to the level's move_player_to_door() method, ie. one connection per door.

Additionally with the titlescreen, I would like to preview an image of each save as the player passes each door. (A screenshot is saved when the player saves the game).
Currently, I have an Area2D (called savePreviewTrigger) under the saveDoor. I was thinking of grabbing the on_body_entered signal from the savePreviewTrigger, and re-emitting it from saveDoor.gd as a saveSelected signal where I would pass the current door, door name, or door(save) number.

How should I go about implementing these areas? My current proposal looks like bubbling signals to me, which goes against one of the rules I read here: https://www.gdquest.com/tutorial/godot/best-practices/signals/

  • Yes, you'd have to connect them in a loop. If you have 10 buttons with the name "button_1", "button_2" you can do this:

    for i in range(1, 10):
        var button = get_node("button_" + str(i))
        # do something with the button

    You can also get the parent of the button and get all the children (assuming that node only has those buttons as children).

    var button_holder = get_node("button_holder")
    for button in button_holder.get_children():
        # do something with the button

    Then on the button signal emit you could send the name of the button, a number, the button itself (self), an enum, or whatever to set them apart.

Yeah, bubbling signals is probably not great as it adds more coupling and requires more work if you add/remove objects from the chain.

Usually what I do with buttons is have one function on a main script (like on the title screen script) and each of any number of buttons connects to a single signal that does to the one main function. Signals can contain an arbitrary number of parameters. So you could call a signal like:

emit_signal("door_touched", self.position, "level_2.tscn")

Or whatever. Then the door can move the player to the position and then load level 2.

  • al1f replied to this.

    cybereality
    The main script would be connecting to each individual button in a loop though? That is what I have for normal(not saveDoor) doors right now.

    How would I go about receiving the information about which Area2D was entered? I would need a script that would emit a custom signal to pass the number of which save preview to show. (The number can be set from the main script when creating them).

    Yes, you'd have to connect them in a loop. If you have 10 buttons with the name "button_1", "button_2" you can do this:

    for i in range(1, 10):
        var button = get_node("button_" + str(i))
        # do something with the button

    You can also get the parent of the button and get all the children (assuming that node only has those buttons as children).

    var button_holder = get_node("button_holder")
    for button in button_holder.get_children():
        # do something with the button

    Then on the button signal emit you could send the name of the button, a number, the button itself (self), an enum, or whatever to set them apart.

    How would I send the name of the button in the script? I'd need a custom script for the button that passes their name into the signal. I'd need to define a new signal inside the button script as well, right?