Reputation: 530
I started with Godot about 2 weeks ago, But have been getting _load_data: Condition !f is true. Returned: ERR_CANT_OPEN (Godot)
on this code:
extends Area2D
var points = 0
var pointAdder = 1
var pointMultiplier = 1
# Called when the node enters the scene tree for the first time.
func _input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
points = (points + pointAdder)*pointMultiplier
get_node("../scoreLabel").text = str(points)
Node tree:
Spatial (Node)
├─backgroundMap :: TileMap
└─scoreLabel :: Label
├─treeClickableArea :: Area2D <<📜
├─treeSprite :: Sprite
└─treeCollider :: CollisionShape2D
I am trying to display the number of times that treeCollider
has been clicked. When I launch the game despite the error, It will count up no matter where I click.
Upvotes: 3
Views: 1576
Reputation: 801
Ok, this is kind of a workaround, but this WILL work (I have tested on Godot 3.2)
Keep your same node setup - final code looks like this:
extends Area2D
var points = 0
var pointAdder = 1
var pointMultiplier = 1
var mouseover = false
func _input(event):
if (mouseover and event is InputEventMouseButton && event.pressed):
points = (points + pointAdder)*pointMultiplier
get_parent().get_node("scoreLabel").text = str(points)
func _on_Area2D_mouse_entered():
mouseover = true
func _on_Area2D_mouse_exited():
mouseover = false
As you can see from the bottom two functions, you will have to connect 2 signals to your Area2D: mouse_entered()
and mouse_exited()
. When it asks what node to connect to, connect to itself (choose the same Area2D).
To make this work, I've added the variable mouseover
and set it to false. For the mouse_entered()
signal, mouseover
gets set to true
. For mouse_exited()
, it gets set to false
. This will track whether the mouse is actually over your area before you click. When tested, the scoreLabel
counts up when the Area2D
is clicked on, but not when clicking anywhere else.
I know this is kind of a hackish solution - I've seen better proposed, but they don't seem to be working for this case (label counts up no matter where you click). At least this method works for sure.
Hope this helps.
Upvotes: 2