Nested if statments and elif not working as inteneded

Godot Version

godot 4

Question

godot runs through all if statements even if the condition isnt met at the beginning but is after a certian if statement changes it. its hard to explain so heres an outline:

var a = 0
var b = 1
if Input.is_action_just_pressed("foo"):
     if a < b:
          a = 2
          print("a = ", a)
     elif a > b:
          a = b
         print("a = ", a)

if i run this in godot it will print a = 2 AND a = 1 even though a started as 0 so the elif never should have executed.
this happens no matter where in the script i put this.

i asumed elif worked like c#'s else if statement

This is weird. I am also seeing this issue and it seems like it’s long running.

i just read that post, and his if-elif is also triggered by input events, although coded slightly different. i wonder if the if-elif act differently since inputs, or events in general are involved. ill have to try this in a way that doesnt involve input or events.

Ok i had closed godot out of frustration. i just opened it to try it without the input, but just to give it another go i hit play …and it works now.

taking turn it off and on again to a new level here godot.

I’ve noticed some things don’t perform correctly without a recompile so I might keep that f6 key at the ready at any point when you dont see the expected result you think you should have during changes. It seems wild to me that having to close and reopen the program would resolve this but who knows.

godot isn’t alone in this, its been rare but unity does it too. though its usually with prefabs and components. i had to redo a project because unity insisted one of my prefabs must have a rigidbody on it when i hit play, even after removing all components exept the mesh before hitting play.

I copied your code into python which looks super similar to gdscript and it doesnt perform like that. It escapes the code as expected which this script should do as well. If you need to interact with inputs again, it might be worthwhile to try separating it out to another function and returning on a successful if enter if you are modifying the variable evaluated later in the if/elif/else statement.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.