oneshot signal not working

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

Hi Community!

In my game the player can drop tiles to build a tower. The tiles are RigidBody2D nodes and hence use the physics engine. The tiles are created on clicking LMB and are instanced as children of my level. Dropping tiles should only be possible when the prior tile is on the ground and no longer moving.

So I want my tiles after entering sleep mode to emit a signal to my level node to make it possible to drop the next tile. The signal should only be emitted once as soon as the tile comes to rest. It is possible that later tiles force prior tiles to move again and at this point the “old” tiles should NOT emit this signal again.

The signal code in my

func _on_Tile_sleeping_state_changed():
    emit_signal("sleeping_now", CONNECT_ONESHOT)
    print ("Going to sleep now! (TILE)")

The code in my

func _input(_event):
    if Input.is_action_pressed("left_mouse_button") and timer.is_stopped():
	    if dropped_tile_asleep == true:
    		dropped_tile.connect("sleeping_now", self, "on_tile_is_sleeping")

func on_tile_is_sleeping(tile):
    print ("OK, good night! (LEVEL)")
    dropped_tile_asleep = true

The signal works, but the oneshot thing does not. The print entries tell me, that the signal also fires again when tiles are moved by other tiles. Did I misunderstand how CONNECT_ONESHOT works or is my code wrong?

Thx for reading

:bust_in_silhouette: Reply From: RandallCurtis

CONNECT_ONESHOT is a connect() parameter, not an emit_signal() parameter. emit_signal() only accepts Variant arguments to send along with the signal. So your code should work with only a few changes:

emit_signal("sleeping_now", self)

dropped_tile.connect("sleeping_now", self, "on_tile_is_sleeping", CONNECT_ONESHOT)

Thank you for your answer. I also tried it this way round before, unfortunately it didn’t work. I’m sorry that I didn’t already mention this and support the error message.

However, I found a solution for my problem. I made on_tile_is_sleeping(tile) a oneshot via editor and this worked perfectly for me.

Cavas | 2022-11-09 18:10