im still dumb, and signals are very confusing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By uh no

I have a node called office, which instances clone of computer(named c in code). how to I command the clones to emit signals (goal & shit) to signals within office script.

–relevant code from office–

var computer = preload("res://computer.tscn")
func _on_spawntimer_timeout():
var c = computer.instance()
add_child(c)
:bust_in_silhouette: Reply From: Mr. Gamezz

I might need more elaboration, Does the timer actually start? If the timer does start, You might need to tick the One Shot box on the spawn timer. Try checking both OneShot and AutoStart on the timer node.

yes, the timer works, It is a standard timer start in the _ready func, the timer has no one-shot, as it loops to spawn multiple enemies

uh no | 2022-01-19 13:02

:bust_in_silhouette: Reply From: Gluon

If you want each of the instances of “computer.tscn” to produce a signal you will need a script attached to your scene of computer with an emit signal line.

so for example you might have

signal Computers_Signal

func _ready()
    emit_signal("Computers_Signal")

the first signal with the name is at the top of your script and sets up a signal attached to that script with a given name. the emit_signal sends that signal out (in this case when the scene is first set up).

You will then need a different scene to listen out for those signals and for that you use connect so another script somewhere will have a line like this

$Computer.connect("Computers_Signal", self, "function_to_run")

func function_to_run()
     do something here.

I hope that helps but if not the godot documentation with signals can be found here

:bust_in_silhouette: 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