|
|
|
 |
Reply From: |
DaddyMonster |
It’s perfectly normal for things to take a while to click. Signals especially because they’re all a bit abstract. It’s the same for everyone; if you’re dumb, we all are!
What are signals anyway?
Signals are a way for nodes to communicate. Specifically, one node can call a method (function) in another. So, your timer node can call _timeout()
in another node when it’s finished counting down.
Why not just use get_node?
I’m sure you know that you can call a method with $ExampleNode.example_method()
or get_node("ExampleNode").example_method()
But what happens if the node is removed from the tree or moved? It crashes, that’s what. This sort of code can easilly result in “spaghetti code” - did kind of direct referencing going all through your code means that it breaks easily and debugging is a nightmare.
What makes signals different?
Signals aren’t like spaghetti. They’re not a brittle connection. They’re more like broadcasting over radio. One node can broadcast on a certain frequency and another node can listen in. If you move or delete the listening node, the broadcasting node doesn’t care.
You can now do what great programmers dream of: you can keep a separation of concerns in your code. So your GUI menu code for example is completely self-contained, it doesn’t rely on anything else.
If there’s a bug with the menu you know the problem is in your menu code, you don’t need to chase variables all across your project looking for the problem.
That’s the general idea. Gluon left a really good answer dealing with the specifics of how to use them in code. Do take a read through the docs that Gluon linked.
Still with it, you’ll get there!
This question isn’t mine, but it’s definitely an issue I’ve struggled with, and this answer was super helpful, thanks!
samjmiller | 2022-01-19 17:14
Welcome! Delighted you found it helpful.
DaddyMonster | 2022-01-20 01:09