0 votes

This is a little oddity I came across when trying to pass inputs between process and _physicsprocess, 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 isonfloor(). 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.

in Engine by (436 points)
edited by

1 Answer

0 votes

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
by (509 points)

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.

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 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 webmaster@godotengine.org with your username.