So I've spent the last few hours trying to get some sort of keyboard handling working, and I can't seem to find out why it's not. I didn't really want to post here, as I have another idea that I'm pretty sure will work, but I quite like the system I'm currently using, and would like to use it. Basically my problem is that I'm trying to use love.keypressed() to add any keys pressed to a table, and love.keyreleased() to remove keys, and then make another method that loops through that list when called, and returns whether a given key is pressed for that frame.
The problem is that somewhere along the line, that list is being cleared. I'm guessing this is to do with references (I'm directly adding key from love.keypressed(key) to the table, does it get nullified after the method ends?) If you want me to post any code, I will (But this post is more of a question of how to get around references to key), or if you want me to explain my issue more (I don't think this post has been very clear), I will. If you know how to get around these references (Or if it's actually possible), say, but don't spend ages looking for the answer. Thanks!
Keyboard handling not working
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Keyboard handling not working
Code: Select all
keystate = {}
function love.keypressed(key,scancode) keystate[scancode] = true end
function love.keyreleased(key, scancode) keystate[scancode] = false end
but yes, do post your code since from what i've read, you most certainly don't erase the contents of the table... unless you define it in love.update or keypressed or something, since in that case, it will always be cleared when those functions get called.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Keyboard handling not working
What zorg said. I presume you're doing something along these lines:
but as you can see, it works without problems, so there's something else in your code that is somehow clearing the table. Anyway zorg's advice about using isScancodeDown is quite sound. And if you definitely want a table, then consider also his advice of using the keynames as indices instead of using the table as a list. The only advantage of a list is that it preserves the order in which the keys were pressed, while they are held, but that has seldom any use. Perhaps a fighting game with combos that require the keys being pressed in a specific order and held, or something like that.
Code: Select all
local keys = {}
local function down(k)
for i = 1, #keys do
if keys[i] == k then return i end
end
return false
end
function love.keypressed(k)
if not down(k) then table.insert(keys, k) end
end
function love.keyreleased(k)
local index = down(k)
if index then table.remove(keys, index) end
end
function love.draw()
love.graphics.print(table.concat(keys, "\t"))
end
-
- Prole
- Posts: 9
- Joined: Thu Jul 19, 2018 7:52 pm
Re: Keyboard handling not working
Thanks for your response! I did consider a similar approach to what you posted, but I think I'll stick to what I've currently got, unless I spend ages on it.
Some of the code is over-engineered, but the general layout of the code should be fairly obvious. The main files to look in (I think, anyway) are world.lua (States/Gamestate/world.lua) and input.lua (Models/input.lua). Thanks for helping!
P.S. Don't get a heart attack when you see what's at the bottom of world.lua. I promise, I didn't type them all out manually, I made an auto-tiler, something I'm very proud of
I've attached a code file. Look through it if you can, but please don't spend ages looking for the problem.
Some of the code is over-engineered, but the general layout of the code should be fairly obvious. The main files to look in (I think, anyway) are world.lua (States/Gamestate/world.lua) and input.lua (Models/input.lua). Thanks for helping!
P.S. Don't get a heart attack when you see what's at the bottom of world.lua. I promise, I didn't type them all out manually, I made an auto-tiler, something I'm very proud of
- Attachments
-
- topdown1.love.zip
- (16.95 KiB) Downloaded 61 times
Re: Keyboard handling not working
What makes you think that the table is being cleared? I see nothing wrong with the table. When pressing a second key while holding the first, it correctly prints the held key in love.keypressed (after disabling the print in input:update to not confuse both).
On a secondary note, the case in Sprites/Entities/player.lua is wrong for the require of Models/input. The 'i' of 'input' is in upper case.
On a secondary note, the case in Sprites/Entities/player.lua is wrong for the require of Models/input. The 'i' of 'input' is in upper case.
-
- Prole
- Posts: 9
- Joined: Thu Jul 19, 2018 7:52 pm
Re: Keyboard handling not working
Yeah, it was that. I guess because I corrected a typo in that line already (There must've been 2), I didn't think myself stupid enough to have made another. Sorry for wasting your time :/ But thanks anyway!
Who is online
Users browsing this forum: bostonstrong567 and 7 guests