public bool WantToUseSpecialAttack()
{
return Godot.Input.IsActionPressed(_attackSpecial);
}
And then I’m polling for inputs in _Process:
public override void _Process(double delta)
{
//do irrelevant stuff
if (_input.WantToUseSpecialAttack())
{
_weapon.UseAttack();
}
}
The problem - rarely, but consistently IsActionPressed will keep returning true once key is no longer pressed (key must be pressed first, it won’t return true without input), and it will keep returning true until key is pressed again.
Is there anything that I should know about? I have found some post from year ago that doesn’t exactly mention this issue, but from what I understand IsActionPressed is buggy (due to certain limitations, it’s not bad design on Engine’s side) and should not be used. But I want to be sure before I try to come up with solutions that will inevitably increase complexity of the code.
Without seeing the rest of your code, I’m going to guess that you are wrong about IsActionPressed and the problem is elsewhere. IsActionPressed has its limitations, but failing to detect when an input is released shouldn’t be one of them.
I’ve already made tests to isolate it. WantToUseSpecialAttack returns true on loop without key being pressed, and it contains nothing but IsActionPressed. I’ve also used external script that monitors shift key (action is tied to shift at the moment) to confirm that it’s not the case of key being physically stuck or some Windows glitches.
I’ve never had this happen to me before, and I’d have to agree with @rainerdeyke. While your snippets are simple, they don’t provide all the code necessary for us readers to comprehend your program; we can only guess.
That said, a few things come to mind:
Have you checked that your keyboard isn’t malfunctioning?
Have you tried binding the action to another key? SHIFT is a modifier key so perhaps that causes the issue?
If the keybord is broken then script would catch it. But I never noticed any issues with shift button before.
In fact I did not. The issue here is that for my use case shift is the perfect default keybind (it can be pressed without moving fingers from WASD and space). But I can run some tests just to be sure.
I’ve already implemented simple workaround - I’m using IsActionReleased and IsActionPressed from InputEvent to switch local booleans, and those WantTo methods just return the booleans. But I did not test it enough to confirm that it works properly now, trying to trigger this glitch is quite mind numbing.
The suspicious part - as I’ve just said I had more methods that rely on IsActionPressed. And only the one tied to shift has this behavior. Other two actions tied to mouse buttons never glitched. I should have said that earlier but I’ve realized it just now.
I’m in the same boat and I spent days analyzing it and did several tests and I can confirm: the behavior ONLY affects the SHIFT key, not ALT, not CTRL, just SHIFT. Gamepad bindings are fine, it’s only IsActionPressedin combination with SHIFT. I also did try the anti-ghosting demo from Microsoft and since I own a mechanical “gamer” keyboard, it’s all fine. Sticky keys are turned off and obviously the SHIFT key works fine in other games (and other engines).
So if anyone has a solution for that it would be really great! And no, GetAsyncKeyState(VK_SHIFT) is not an option since I have the option of rebinding keys in-game, I won’t hardcode VK_SHIFT into my project.
Using _input() is not an option since using that only triggers the “run” action once and while the Shift key is pressed, the “run” state gets reset. Also I implemented it like in the docs where _physics_process() is used. I also checked some basic FPS examples in the asset library and those with a “run” or “sprint” option bound to the shift key, also have that buggy behavior that the key gets stuck.
I digged through the engine code but I’m not C++ programmer. Regarding other resource, it seems that the shift key is just treated differently as ctrl/alt and it may have to do with synthetic events, i.e. pressing one key plus the shift key and then releasing the shift key reads the “pressed” state still as true.
I could use Input.is_physical_key_pressed((Key.Shift) which is working but that would prevent me from making re-binding available. Also there are many issues on Github and this may be a related one (still open).
It is, in fact, an option but it looks like you would rather complain about the bug.
I’m not sure what you mean by the “run” state being reset. Could you elaborate?
If you want something to be called continuously while a button is being pressed, you can use a bool variable to store the state of the input. The variable can then be used inside _process() to conditionally run blocks of code.
var sprint: bool
func _process(delta):
if sprint:
# PERFORM SPRINTING
func _input(event):
if event.is_action_pressed("your action"):
sprint = true;
if event.is_action_released("your action"):
sprint = false;
If this doesn’t work for you, feel free to post any further questions.
I’m sorry if it looked like I only want to complain, not my intention.
So, yes, I implemented it exactly as you wrote (thanks for your example) and this doesn’t work.
What happens:
You press/hold the “direction” key (W/A/S/D) and then hold the shift key to sprint
Sprinting is activated but stops right after 0.5 seconds or so (walking in the direction remains), although the shift key is still hold
That’s all. I played around with various approaches but nothing worked. I just implemented as various other controllers like this FPS example. So, no _input involved() but if you try it and play around with holding/releasing the shift key (bound to sprint), it gets stuck eventually and you keep running.
It really should work, check your keyboard for ghosting; press multiple keys including shift, make sure every key you hold down lights up, a good keyboard can press around 5 keys at a time
Unfortunately, it isn’t a ghosting issue, as I said before. I hold only two keys (W plus SHIFT) and it already happens, no multi-keys involved. I already tested that and obviously I play first-person games and can sprint successfully in those games. Also, the issue ONLY persists with the SHIFT key, CTRL and ALT (or any other key as sprint key) are working totally fine!
0.5 seconds is a rather odd length of time – kinda similar to the time it takes for input echoes to appear. I’m not sure what to make of that.
Well, we’re trying to solve your sprint problem here; not the one that’s present in some other guy’s repo.
Could you show us the code you’re using to implement the sprint along with the input code you’re using? It’s much harder to diagnose your system without having access to the code in question.
Thanks a lot for providing the example, I rebuilt it and adjusted my code to your _input logic again and now it seems to work although I tried the same in the afternoon and it didn’t work.
I will check it further but for now, this is the solution!