Why is my object not shrinking on click?

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

I have an Alien Planet object that I want to increase in scale when it is hovered over and decrease when the mouse is moved away. However, if this planet is clicked on, I want it to stay big even when not being hovered over, and then be deselected when clicked on again or when clicking anywhere outside the planet. The problem is the planet doesn’t shrink back down when clicking outside the planet to “deselect” it. What am I missing in my code?

extends Node2D


var scale_factor = Vector2(.75, .75)
var hovered = false
var selected = false


func _on_PlanetArea_mouse_entered():
	if not selected:
		hovered = true
		self.scale += scale_factor

func _on_PlanetArea_mouse_exited():
	if not selected:
		hovered = false
		self.scale -= scale_factor

func _input(event):
	if event.is_action_pressed("click") and hovered:
		selected = !selected
	elif event.is_action_pressed("click") and selected:
		selected = !selected
		self.scale -= scale_factor

It looks like it really should work… place some print statements after each of your conditional to see it something is wrong. Try using selected = true/false instead of !selected.
This kind of problem would be completely normal for Control Nodes, since they don’t read input outside of their borders. But You are using Node2D ?

Inces | 2022-01-22 08:43

Yes, try that so you know if the script is running or not. Also check if you connected the signals.

Shiva | 2022-01-22 16:47

Ah, this helped me narrow down the problem! The issue was that after “selecting” a planet, it would no longer set hovered to false after moving the mouse away, so I had to move the hovered = true and hovered = false BEFORE the if statements in the signal functions. Thank you for the suggestion!

YangTegap | 2022-01-22 17:53

:bust_in_silhouette: Reply From: DaddyMonster

I just recreated the project and it works perfectly. I set up my tree like this.

Node2d
    Area2D --> Signals connected to Node2D
        CollisionShape2D
    Node2D --> Your script
        Sprite

You’re not scaling the Area2D are you? Double check you connected the signal _on_PlanetArea_mouse_exited signal, make sure there isn’t a typo in the name.

Thank you so much for looking into this! Turns out the issue was that after “selecting” a planet, it would no longer set hovered to false after moving the mouse away, so I had to move the hovered = true and hovered = false BEFORE the if statements in the signal functions.

YangTegap | 2022-01-22 17:54

Oh. You know what, I suspect I might have fixed that without thinking and then proceeded to forget I’d done it… Oops. Old age isn’t being kind! Anyway, great that it’s fixed!

DaddyMonster | 2022-01-22 19:48

:bust_in_silhouette: Reply From: YangTegap

The issue was that after “selecting” a planet, it would no longer set hovered to false after moving the mouse away, so I had to move the hovered = true and hovered = false BEFORE the if statements in the signal functions.