Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.