This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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
Godot version 3.4.2
in Engine by (93 points)

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 ?

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

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!

2 Answers

0 votes
Best answer

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.

by (93 points)
+1 vote

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.

by (2,159 points)

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.

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.