|Attention||Topic was automatically imported from the old Question2Answer platform.|
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