Panel bg_color Tweening with / without signal

Godot Version




I’d like to make a “fade out” effect when the player collides with some collider. Here is my fadeout script which is attached to a Panel

extends Panel
var style = StyleBoxFlat
var color
var tween: Tween

# Called when the node enters the scene tree for the first time.
func _ready():
	style = preload("res://fadescreen.tres")
	color = style.bg_color
	tween = get_tree().create_tween()

func fade_out():
	tween.tween_property(style, "bg_color:a", 1, 0.15)

func _on_event_area_fade_out():

If called from _ready(), the fadeout will occur with no other problems. However, a problem occurs if called using a signal from the script attached to the Area2D…

extends Area2D

signal fade_out

var player = null
var playerset = false
var player_scene

func _ready():
	player_scene = preload("res://player.tscn")

func get_player():
		player = get_node_or_null("/root/StartScene/FloorGroup/Player")
		if player != null:
			playerset = true

func _on_body_entered(body):
	if body == player:

Trying to call from the signal gives a couple of errors:

E 0:00:00:0854   step: <Tween#-9223372004290198108>: started with no Tweeners.
  <C++ Error>    Method/function failed. Returning: false
  <C++ Source>   scene/animation/tween.cpp:319 @ step()


E 0:00:03:0497 @ fade_out(): Tween invalid. Either finished or created outside scene tree.
  <C++ Error>    Condition "!valid" is true. Returning: nullptr
  <C++ Source>   scene/animation/tween.cpp:104 @ tween_property()
  <Stack Trace> @ fade_out()
        @ _on_event_area_fade_out()
        @ _on_body_entered()

I can fix this by calling _ready() in response to the signal before calling fade_out(). Using that makes things work perfectly (for now) but I know it’s not the right thing to do, calling _ready() manually is probably not recommended. Furthermore, I’d just like to understand what’s going on a bit more by solving this problem .

Is there any insight you can offer into this? Also feel free to offer suggestions and corrections to other parts of the scripts.

Thanks for reading and thanks in advance for your replies.

You need to create the tween whenever you need it or it will be disposed before you can use it.

Move your create_tween() inside the fade_out() function.

Dang i feel silly now lol.

Thanks for the help!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.