Invalid operands 'Object' and 'bool' in operator '=='.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By swordofbling
extends KinematicBody2D
onready var key = get_node("/root/blob")

func _process(delta):
	if key == false:
		$Sprite/AnimationPlayer.play("closed")
	if key == true:
		$Sprite/AnimationPlayer.play("open")
:bust_in_silhouette: Reply From: KijeviGombooc

I believe get_node() returns a node, which is not a boolean, so it cant be true or false. What you might want to do is wether the node is found, you can check it like this:

if key == null:
    #node isnt found
else:
    #node found
:bust_in_silhouette: Reply From: Zylann

I’d like to add you should not use get_node on an optional node. The reason is, get_node will produce an error if the node isn’t found, and it can confuse you down the road because such error could be a mistake in your code, it’s harder to find them if you have to “ignore errors that are actually fine”.

For optional nodes, use has_node instead, then if it succeeds, use get_node if you need the node.