3

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.

CATboardBETA
  • 418
  • 6
  • 29

1 Answers1

2

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.

Christopher Bennett
  • 803
  • 1
  • 8
  • 20
  • Ok, I think this should have worked. Although, it still does not count and gives the same error. (_load_data: Condition "!f" = true. Returned ERR_CANT_OPEN.) In case this helps, here is a screenshot: – CATboardBETA May 12 '20 at 01:20
  • I see you typed the code, but it looks like you didn't actually connect a signal. If you click on the Area2D node (treeClickableArea), and then look at the top of the Inspector window, you will see 2 tabs: Inspector and Node. Click the Node tab, and you will see all the signals. Double click on the one you want and follow the steps. Rinse and repeat for the other. When connected correctly, there should be a little "wi-fi" looking logo next to your Area2D. – Christopher Bennett May 12 '20 at 01:31
  • Also, I just noticed, in your picture, you forgot the line `points = (points + pointAdder)*pointMultiplier`. – Christopher Bennett May 12 '20 at 01:49
  • Oh, and change `("/scoreLabel")` to just `("scoreLabel")`. – Christopher Bennett May 12 '20 at 01:51
  • oh I was trying to see if anything was different – CATboardBETA May 12 '20 at 02:24
  • I need to connect a the mouse_entered() and mouse_exited() signals under the id _on_Area2D_mouse_entered and _on_Area2D_mouse_exited, correct? (I haven't ever worked with signals before :-/ ) – CATboardBETA May 12 '20 at 02:33
  • Yeah connect the signals. When it asks where to connect, choose itself, and put the mouseover true/false under the code it provides by the signal. (ex: `func _on_Area2D_Mouse_entered()` ). Just like in the code above. As a side note, If you plan on continuing with Godot, it's well worth taking the time to learn all about signals because Godot uses them for a lot of things, and they can be tricky (a pain in the ass) to get the hang of at first. – Christopher Bennett May 12 '20 at 02:33
  • which signals do you mean? – CATboardBETA May 12 '20 at 02:34
  • The ones we've been talking about. Select the node (treeClickableArea), choose the "mouse_entered()" signal in the inspector window under the node tab. When asked, connect it to itself, it will take you to the script editor and will have placed the code (if it is't already there) `func _on_Area2D_mouse_entered():`. Under that, write `mouseover = true`. Go back to the node and do the same thing with the `mouse_exited()` signal. – Christopher Bennett May 12 '20 at 02:38
  • Y'know, it would have helped If I wasn't dumb and deeded t put the label underneath of the tilemaps. Sorry about all that. It works perfectly! – CATboardBETA May 12 '20 at 02:42
  • No problem. If you're just breaking into a new engine, get used to head banging moments like that. Glad it finally works for you. – Christopher Bennett May 12 '20 at 02:43
  • I don't suppose there might be an attribute like .text so I can increase the font size too? Or would I have to make my own font? – CATboardBETA May 12 '20 at 02:46
  • As far as I know, Godot is pretty barren when it comes to default font options. To get extra options, such as size, you have to use dynamic fonts. You can select it from the Label node under font in the inspector, and you have to choose "new dynamic font" (bitmap is generic and limited). You have to supply your own font file though. – Christopher Bennett May 12 '20 at 02:58
  • Ok, thanks. I greatly appreciate your help through all of this – CATboardBETA May 12 '20 at 03:07