Why is my input sometimes being eaten?

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

This is a little oddity I came across when trying to pass inputs between _process and _physics_process, and I was wondering if anyone could explain it to me.

To set it up, I’ve defined a variable “jump”

var jump = false

In process I have:

	if Input.is_action_just_pressed("jump"):
	jump = true

Now, if in physics process I have

if jump:
	if is_on_floor():
		exForce += jForce #Read: Do the jump action
	jump = false

then a large chunk of my Jump inputs are eaten, and nothing happens when I hit the button.

However, if I instead have

if jump:
	if is_on_floor():
		exForce += jForce
		jump = false

It works fine, with 0 inputs dropped. The problem with this method is that it allows you to buffer your jump in mid air, something I don’t want to have happen.

I’m not really sure why this is happening. If someone could help me understand, that’d be great!

EDIT: After a little more experimenting, it’s definitely got something to do with the is_on_floor(). If I remove that, I get no inputs dropped.

EDIT2: I solved my issue, however I still don’t really understand why it was happening in the first place. The solution was to define whether the player was on the floor as a variable before the Input was even taken into consideration, rather than have it run the function during the if statement. The working code looks like this:

if is_on_floor():
	canJump = true
if jump:
	if canJump:
		exForce += jForce
		canJump = false
	jump = false

Like I said, I’m still not exactly sure why this happened. I have a general idea to avoid running functions inside if statements like that if something similar happens again though.

:bust_in_silhouette: Reply From: fpicoral

You are not using the right amount of tabs. You should use like that:

if jump:
    if is_on_floor():
        exForce += jForce #Read: "Do the jump action"

    jump = false

I would suggest you to remove all this code, including the variable jump and in the _physics_process() add a function called jump() and then define this function this way:

func jump():
    if Input.is_action_just_pressed("jump") and is_on_floor():
        exForce += jForce

The improper amount of tabs is actually an error I made when typing this post, not an error in the code itself, that’s my mistake. I have the tabs you’re saying, it still has the problem. I’ll edit the post to rectify that.

I’m not 100% on the second part of your post. I’m just starting out with Godot, would you mind explaining how that jump function would actually be called? If I call it every physics loop my inputs still get eaten, so I’m not sure if I’m missing something about what you’re suggesting.

denxi | 2019-01-25 22:39