|Topic was automatically imported from the old Question2Answer platform.
I have a canon which I wish to charge when the player holds the “shoot” button, and fire when the button is released. Following the official documentation recommendations, I handle all gameplay input in the
var canon_charging = false
var canon_fired = false
canon_fired = false
canon_charging = true
canon_fired = true
canon_charging = false
This all works fine, but sometimes releasing the “shoot” button doesn’t cause the player to shoot, and instead the player is stuck on the charging animation. Through print statements to the log I’ve traced the issue to the check in
_unhandled_input for whether the button is released.
Is this a known issue? I thought that the
_unhandled_input function would catch all input events that have not yet been consumed, which for me is all of them since I have not implemented any other input functions yet. If it is known, are there fixes planned?
This problem is mostly solved for me, but for future readers I’ll describe a few things which I was affected by that can contribute to this issue.
My old MadCatz Xbox controller is decent, but it seems as though some buttons stick sometimes. I tried switching to a switch controller, but that presented its own issues which I won’t go into because they aren’t relevant here. Takeaway: be sure the controller is in tip-top shape.
My version of Godot isn’t the most up-to-date, and updating did give different behaviour (the minimal example can be downloaded from here if anyone wants to try).
The most serious issue was a logic error which I’ll explain. In the
_unhandled_inputI was initializing the
falseand only changing it to
trueif the “shoot” button was released (then handling the logic for shooting in the physics process). Normally this would work, but not if another input is captured before the physics process has a chance to run (i.e., such as analog stick input).
I believe this is what was occurring. Moving the analog stick around while releasing the button caused
canon_firedto be set to false again before the physics process could run and call
shoot. However, newer versions of Godot seem like they may call
_physics_processmore frequently, and so they yield different behaviour.
The fix is quite simple: the `canon_fired` variable is now set to `false` inside the `shoot` function (which is better practice anyway) and the result works with both versions of Godot.
I hope this helps anyone who may have run into a similar set of circumstances/made similar errors to me