Could someone tell me what's wrong with this?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JustHaven

Alright, so my plan was to have my character model play a landing animation upon landing. I used a variable, “jumped”, to make sure that the character touched the ground after a jump, so that it won’t loop the animation whenever it’s touching the ground.
if Input.is_action_pressed("jump"):
var _jumped = 1
if is_on_floor() and jumped == 1:
var _jumped = 0
I already solved all the errors it gave me, and it says that nothing is wrong. However, the animation doesn’t play when it lands. I’ve been puzzling over this for almost an hour.

I’m also new to GDScript, so please be nice.

:bust_in_silhouette: Reply From: jgodfrey

A few things jump out at me…

The likely culprit is that you’re using two different variable names (jumped and _jumped). I assume those should be the same variable.

Also, I’m not sure where this code is running, but is_action_pressed() will fire in every frame where the key is pressed. If you only want this to fire once when the action is first pressed, you’d want to change that to is_action_just_pressed().

Finally, your indention is all wrong above, but I assume that’s an artifact of how you pasted the code into the forum. To get it to display better, just cut/paste working code into your message, select it, and press the { } button in the forum editor’s toolbar.

The reason one variable is “jumped” and another “_jumped” is because the Engine told me to do that in the debug menu. I assumed it would work; guess not.
“is_action_pressed()” is me being an idiot, I forgot that there was the two.
Finally, yes, the indentation is just a forum issue, everything’s indented properly in the code.

JustHaven | 2022-04-18 02:01

:bust_in_silhouette: Reply From: SnapCracklins

As mentioned earlier, your _jumped variable has mismatched names. (The second one is named jumped which will misfire)
However i think this is a case of your code firing too quick. You have your jump code but then under that jump code you have the switch. That creates a dependency where you always call jump and probably fire both pieces of code at once because you can’t call the second expression without hitting jump. You likely need to separate them, perhaps with a signal? Put a custom signal and fire it in that is_on_floor() function (what is that and why is it true?) and then when the signal fires then run the second half of the code. You also may have some quirks in your logic with the is_on_floor function() being true and the variable being true. If both aren’t true your event doesn’t happen.

My money is on that jump code though. Break it apart.

signal jump_end

func _ready():
      self.connect(“jump_end”, self, “on_jumpend”)

Then your code:

> If Input.is_action_just_pressed(“jump”):
>        var _jumped = 1

Then put the signal in the is_on_floor() function:


Then the signal function:

func on_jumpend():
       if _jumped == 1:
            _jumped = 0