+2 votes

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)
in Engine by (24 points)

3 Answers

0 votes

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.

by (55 points)

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

+2 votes

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

https://docs.godotengine.org/en/stable/getting_started/step_by_step/signals.html

by (1,417 points)
+3 votes

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!

by (1,789 points)

This question isn't mine, but it's definitely an issue I've struggled with, and this answer was super helpful, thanks!

Welcome! Delighted you found it helpful.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.