0 votes

I have Player1 with a Tween. The parent node is connected to the tween_completed signal via the UI.

I have Player2 which I need to react to the tween in Player1 completing.

I had thought I could simply connect Player2 to tweencompleted on Player1, but this doens't work. Instead I have to emit a separate signal from within Player1, in the ontweencompleted function, which I then capture. I would have thought ontween_completed was a general signal which could be caught by any node in the scene that was interested.

i.e. This doesnt work:

Player1

extends Node2D

export var index:int = 0
onready var tween:Tween = $Tween
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
signal hide_players


# Called when the node enters the scene tree for the first time.
func _ready():
    if index == 1:
        tween.interpolate_property(self, "scale", null, Vector2(2.0,2.0), 4,Tween.TRANS_EXPO,Tween.EASE_IN)
        tween.start()
    pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass


func _on_Tween_tween_completed(object, key):
    self.visible = false

Player2

# Called when the node enters the scene tree for the first time.
func _ready():
    var player = get_node("/root/Game/Player1")

    player.connect("tween_completed", self, "_vanish")


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass
func _vanish():
    self.visible = false

But this arrangment does work, where I emit a fresh signal within Player1.

Player1

func _on_Tween_tween_completed(object, key):
    self.visible = false
    emit_signal("hide_players")

Player2

player.connect("hide_players", self, "_vanish")
in Engine by (42 points)

1 Answer

0 votes
Best answer

Hi,
any object in the sceneTree can connect to any signal from any object in the sceneTree.

I think the problem is

var player = get_node("/root/Game/Player1") # <- this is the player, not the tween

player.connect("tween_completed", self, "_vanish") #the tween emits the signal, not the player
by (3,940 points)
selected by

If my referencing was wrong, neither example would work, would it? In my working example, and non-working example, the node path is the same. The only thing different is the signal. I tried connecting to the /Tween instance directly, that didn’t work either.

This doesnt work.

func _ready():
    var player = get_node("/root/Game/Player1/Tween")

    player.connect("tween_completed", self, "_vanish")


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass
func _vanish(signal_index:int):
    if index == signal_index:
        self.visible = false

in your second example the playerr emits the signal, not the tween. therefor connecting to the player was correct.

In your newer code you connect the tweencompleted with the vanish function. You expect one attribute. But tweencompleted signal is specified like ...

tween_completed ( Object object, NodePath key )

I dont know what index is but its most unlikely the other player object.

Therefor the condition fails and the player did not hide

index is just a way for me to selectively have some instances but not others respond to the signal while I learn...I will try your suggestions and let you know, thanks

Works now, thanks!

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.