Check, if a node (for example a Sprite) is really visible/invisible on screen

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

Hi,

I know I can check the node parameter Node.visible, but this is useless, if one of the parent nodes becomes invisible. Then all childs are invisivle too, but the flag .visible remains true.

Thanks!

edited , now it works fine

Whalesstate | 2021-09-21 08:10

:bust_in_silhouette: Reply From: Whalesstate
func _ready() -> void:
	# connecting signal and adding nodes as extra arguments so we can check if they are visible
	var _ok = get_child(0).connect('visibility_changed', self, '_on_visibility_changed', [[get_child(0)]])
	# child node connected as [ node, node.parent, parent.parent , ....]
	_ok = get_child(0).get_child(0).connect('visibility_changed', self, '_on_visibility_changed', [[get_child(0).get_child(0), get_child(0),]])



func _on_visibility_changed(nodes : Array):
	var visibility = true
	# check nodes from child to parent and update visibility
	for node in nodes:
		# if any node is hidden it will stop and changes visibility to false
		if node == null: return
		if !node.visible:
			visibility = node.visible
			break
	print( "%s visibility = %s" % [nodes[0].name, visibility])

how I answer to a post?

Lebostein | 2021-09-21 08:47

:bust_in_silhouette: Reply From: Lebostein

Thanks. But I found an other solution. There is a method .is_visible_in_tree() for every node, that shows the current visibility!