The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes
func _on_Area2D_input_event(viewport, event, shape_idx):

    var body = false

    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:

       if body == true:

           body = false

       elif body == false:

           on_body = true

So I'm trying to make just ONE block of code run. What happens here is The elif statement would execute first, and then immediately after, the if statement code executes (probably because of the changed variable).

But I thought with if/elif, only ONE block of code runs.

I tried using RETURN but it still doesn't work.

I keep running into this problem and I don't know how to solve it. I'm so confused and would appreciate your help. Thanks a lot!

Godot version 3.2
in Engine by (34 points)
edited by

When something happens and you don't know why, it's an opportunity for debugging, I like to use print(), for example, print("This is if check 1 from node: ",, in one of the ifs, this will allow you to understand what is happenning and potentially how to fix it.

1 Answer

+1 vote
Best answer

Moments like these definitely make you question your sanity as a programmer - we've all been here haha. if/elif does indeed function how you think they do, the block executions are mutually exclusive. The giveaway that something else was amiss should've been when you used return and both blocks still executed because return always kicks you out of a function exactly at that statement.

if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
checks if a mouse button left event occurs, when you click you'll get both the events where event.pressed == true and event.pressed == false. Simplifying your conditionals:

if not (event is InputEventMouseButton):
elif not (event.button_index == BUTTON_LEFT):
elif event.pressed == false:
elif body == true:
   body = false
elif body == false:"animation_name")
   on_body = true

This style of deduction of conditionals helps avoid nested ifs, compound conditionals and generally makes your code much more readable once you get used to it. The bottom two conditions will only execute if the input event is a pressed down left mouse button.

by (3,906 points)
selected by

thANKS man! I found the problem. I forgot or didn't know that having the

event.pressed == true

was necessary. But it makes sense. Because button click is true perpetually even if I click it just once. I'm learning as i go.

Thank you!

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.