#Signal isnt being received correctly (new to godot)

12 messages · Page 1 of 1 (latest)

junior umbra
#

Hello,
im trying to learn how to use godot and im creating a simple game where objects move right to left and the player has to dodge them.

The moving object can detect collisions via the _on_body_entered(body: Node2D) function, as my print statement correctly works.
However when i emit a signal called "hit" within that function and connect it to my player node, any print statements dont work which leaves me to believe that my signal isnt being received correctly.

I have been connecting the signal through the signal tab.
I have tried using movingobject.connect("hit", _on_moving_object_hit),
ive tried resetting the connection multiple times under new names.
i have tried connecting the _on_body_entered function directly to my player.
None of these worked and im having a hard time finding a solution online.

I have included some screenshots of my scripts, connections and game.

Does anyone know why my connection isnt being received correctly? or is there a better way i could be implementing a collision signal?
My goal is to send a signal when there is a collision to stop movement and give the user an opportunity to restart the game.

Any input is greatly appreciated as ive been stuck on this for a couple hours and its becoming quite frustrating.
Thanks

hollow ferry
#

Not sure why it isn't working as is, everything seems right to me at a glance
as an alternative though you can check whether the body in _on_body_entered() is the player and communicate with them that way instead of going through a signal

#

if you're using godot 4 you can also try hit.emit() instead of emit_signal("hit") (no idea why that would make a difference though)

junior umbra
#

i tried the hit.emit() to no avail. but it now works using the body in _on_body_entered() way you recommended, so thanks 😄

hollow ferry
#

Now that I think about it, I might have an idea as to what the problem was:
Each instance of a class has its own signals. When you connect the signal in the inspector, it only works on the one area that's already in the scene. But judging by your screenshot, you're spawning additional areas in code. Those would all need to have their signals connected as well

junior umbra
#

so im my project i am spawning multiple instances of MovingObject1 from a script in ObstacleSpawner.

#

Does that mean i need to connect MovingObject1 signals differently? so i cant connect it in the inspector

hollow ferry
#

you would need to connect the signals in code, yeah
easiest way to do it would be to connect to each new object as you spawn it

last marsh
#

here is how I do it. the bullet has a collider. when it detects the enemy (which is on a specific collision layer so it works only there and doesn't throw errors) the bullet which is an area2d will trigger the body_entered signal. this is connected with a function that is on the bullet own script. so the signal and function is local to the bullet, it doesn't need to know about the enemy.

#

so inside this On body entered i call a function on the body entered in that i called hit()

#

and that one will do whatever task is required on the enemy like applying damage or whatever.