This input thing has become a can of worms...(new demo)
Tue Jan 04, 2022 12:29pm
After reading your post, I decided to try and work on the input and try and hunt down those random stuck key spots. I figured it was from combining using "INP(96)" for movement and "INKEY$" for toggles. So, I moved everything to use "INP(96)" and added a "switch debounce" on the toggle keys so they worked correctly.
Then, everything looked to be working great. In run mode Santa moved similar to Mario in Mario Bros 3 and it was fantastic ...but randomly I would get stuck keys again. This time a bit worse than before. Turns out my INP & INKEY$ flow was working based on loop timing and moving things a little bit exposed how fragile that whole set up was.
So... now I have it working down to a point where everything works correctly except if you very quickly tap an arrow key, it'll sometimes get stuck on. I was just about out of ideas on how to track that down when I re-read your message and saw your suggestion of validating the keypress at the handler before acting upon it. I think that might fix the problem! I'll give it a try and report back. Thanks for that!
Btw, not using a stack for the input. I believe INKEY$ gives you the buffered input in a FIFO style stack but it's VERY slow. INP(96) gives you the current value at polling time of the keyboard port. So, if I'm not polling while a key is pressed, I miss it. There's no way to really put a handler monitoring the input besides the "KEY" command but that doesn't read chained keys.
With INP(96), I'm polling a couple of times during the game loop (before and after possibly large code branches, ex: going to the enemy movement handler). When I'm polling, I have an array that acts as a series of switches of TRUE/FALSE for each valid key. If I read the key is on, I turn it on, if I read the key is off, I turn it off. When it gets to the code that handles the input, it acts upon the flags set in this array.
In the last demo I posted, I used an array of all 128 key codes. I realized I didn't need that many active keys and memory is expensive, so I moved it down to just I think 7? That reduced it from 256 bytes to 14 bytes.
The key codes get a little complicated.
KEY CODE BY ITSELF - key pressed
KEY CODE + 128 - key released
KEY CODE 170 - no key press detected.
The KEY CODE + 128 isn't really super reliable on fast key presses (seeing that I have to poll as much as possible). 170 stinks because it will randomly pop in when pressing a second key down like left arrow + space bar which will stop the left arrow but keep the space bar call, making it impossible to jump.
Annnnnnnnnnnnnyway. I'll try re-validating the input before acting upon it and see how it goes. :)
EDIT: Hey!!! That looks like it fixed it! I haven't been able to get the keys stuck yet with this updated way. (I possibly saw "something" while running into the Elf, but it might have just been me moving as well).
times when it felt like the arrow key "stuck" as some other action was happening.
With the slower enemies, it's possible to "stalk" one as you work your way through the level. That really saves on Christmas Spirit. I think real Santa would sneak and only use Christmas Magic (Blend, Run) when he... more
This input thing has become a can of worms...(new demo)- Erik_,Tue Jan 04 2022 12:29pm
Can keep on delivering but can't end the level. How I did it: Delivered a few presents to the tree then asked the Elf for help. Gave a couple to snoopers, then tried to end level.
Ok, so here's what happened in more detail. With presents to deliver n-1, grab a present and touch the elf. He'll d... more