#How To Write A Signal?

1 messages · Page 1 of 1 (latest)

timber sail
#

I know what signals are for and how they transmit information. That's what every google result is about. I want to know how to put one together, because no page seems interested in that. How do I fix this signal so it gets noticed?

I want to make a dynamic calendar screen, but I don't want it updating every frame, that's not optimal. Instead, I want to use a signal to tell it to update when it opens. The days on the calendar are Panels in a GridContainer. In the button that opens the calendar, I have this code:


signal calendarupdate

func _ready():
    var button = self
    button.pressed.connect(self._button_pressed)

func _button_pressed():
    %CalendarPanel.visible = true
    %ButtonsGrid.visible = false
    calendarupdate.emit()```
And in the class script for the panels, this code:
```extends Panel

class_name CalendarPanel

signal calendarupdate

func _ready():
    calendarupdate.connect(update)

func update():
    queue_free()```
Simple enough. I'll know if it works when I open the calendar and there's nothing there. Then I can modify `update()` to actually do what I want. But nothing is happening...

Are they each looking for their own separate `calendarupdate` signal? That seems to defeat the point of having a broadcast like this instead of directly running functions. But if I don't redefine it, `update()` can't find it... do I need to specifically tie it to the button that sends the signal? (That seems to ALSO defeat the point, I'm using a signal so I *don't* need to jump through all of Godot's weird hoops to have the instances know where each other are.)
pastel ivy
#

defining calendarupdate twice is creating two different signals. one on the script that extends button - one on the script that extends Panel. they are not related.

To connect to another scripts signal you need a reference to that node. this is easy if its in the scene tree.

assuming a tree looks something like this:

Panel
-Child1
--Button (has signal calendarupdate)

you can either in code on the Panel script

@onready var button: Button = $Child1/Button

func _ready() -> void:
  button.calendarupdate.connect(_on_button_pressed)

func _on_button_pressed() -> void:
  print("I was pressed!")

Or in Editor press on the node that has the signal - Click the "Node" tab in the Inspector, where you find built in signals already. and you should see the signal you defined there, and connect it to the Panel script like you would with any builtin signal.

#

Things get a little trickier if youre try to use signals across different scenes - this is where some people would suggest using something called an Eventbus / Signalbus. which is just an Autoload node that contains the defined signal. so you can emit / connect to it from anywhere.

timber sail
#

I'll try it in my master autoload then

#

Hm. Out of 35 panels... one disappeared. That's progress?