0 votes

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 tile.gd:

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

The code in my level.gd:

func _input(_event):
    if Input.is_action_pressed("left_mouse_button") and timer.is_stopped():
        if dropped_tile_asleep == true:
            ...
            $Tiles.add_child(dropped_tile)
            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
Cavas

Godot version v.3.5
in Engine by (12 points)

1 Answer

0 votes

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:
tile.gd:

emit_signal("sleeping_now", self)

level.gd:

dropped_tile.connect("sleeping_now", self, "on_tile_is_sleeping", CONNECT_ONESHOT)
by (104 points)

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 ontileis_sleeping(tile) a oneshot via editor and this worked perfectly for me.

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.