Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Godont |
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 _unhandled_input
function:
var canon_charging = false
var canon_fired = false
func _unhandled_input(event):
canon_fired = false
if(event.is_action_pressed("shoot")):
canon_charging = true
elif event.is_action_released("shoot"):
if canon_charging:
canon_fired = true
canon_charging = false
func _physics_process(delta):
if canon_charging:
charge_canon()
if canon_fired:
shoot()
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?
EDIT
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_input
I was initializing thecanon_fired
boolean tofalse
and only changing it totrue
if 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_fired
to be set to false again before the physics process could run and callshoot
. However, newer versions of Godot seem like they may call_physics_process
more 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