0 votes

So I am building a carnival style whack-a-mole (i know, crazy complicated, but starting simple has helped me learn a lot of the fundamentals) and I have an enemy object listening to the player object (and signaling to the points collection object) for registering hits. Whenever i save the enemy scene and instance it, my signals i connected in the editor no longer function (despite all of this enemy object using the same script).

I am trying to wrap my head around how to connect these instanced nodes within their code as I am sure that is the solution, likely a line in func ready() or func _onScenetreeentered(). I have read the docs and tried to implement the code and I can’t seem to get this important idea down. I understand (hopefully, do correct me if i’m wrong!) that moving up a SceneTree Godot is a tougher task than moving down the tree?

So in short: how do you implement connecting signals within code, to objects AND from objects, when you are instancing a PackedScene?

in Engine by (534 points)

2 Answers

+1 vote
Best answer

You propably connected only one particular mole from the starting scene to the listener.

It is better to disconnect it there, and code it in mole script :

onready var listener = #reference to your listener here 
func ready():
        connect( "whack", listener, "on_whack")

first argument is signal name, 2nd is node to listen, 3rd is a name of function in listener that will trigger. You can also add 4th argument and more, they will be passed as additional information. For example You can pass reference to a mole being hit by entering self as 4th argument. The same can be done when emitting signal :
emit_signal("whack", self)

by (7,755 points)
selected by

Thank you for enlightening me. Sometimes I need a bit more info than the docs and visual examples help outside of full game tutorials. Many thanks! I will give this a go.

+1 vote

You can connect the signals through code in the script of the parent of your elements.
For example this is your tree after instancing:
World
|_ Enemy
|_ Player

As the World node is a mutual parent for both of the Enemy and player scene, It'll be easy to locate them, If you attach a script to the world node, Connecting signals should be much easier. This article can help you too, Make sure you take a look at it

by (96 points)

That was insightful. It also helped me to isolate another problem with how my trees are built and how i test/code instancing. Thanks for that. Nitty gritty details like this are usually my weakness.

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 Frequently asked questions and 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 [email protected] with your username.