AggrorJorn Posted January 19, 2015 Share Posted January 19, 2015 I have a level with 2 lua scripts. However only 1 KeyHit (or mouse hit for that matter) returns true in a single frame. --Script1 in UpdateWorld if KeyHit:Key.Q then System:Print("You are in script 1") end --Script2 in UpdateWorld if KeyHit:Key.Q then System:Print("You are in script 2") end I am not flushing any keys. Is this intended behaviour or am I overlooking something obvious? Quote Link to comment Share on other sites More sharing options...
josk Posted January 19, 2015 Share Posted January 19, 2015 I noticed something like this the other day, the last item added to your scene will have the KeyHit that is read. Not tested this properly but that was what I found. Quote Elite Cobra Squad Link to comment Share on other sites More sharing options...
Ma-Shell Posted January 19, 2015 Share Posted January 19, 2015 You can work around this by saving the key's state of the previous frame (KeyDown) and then acting, if the current state is "not down" and the previous one was "down". Quote Link to comment Share on other sites More sharing options...
Rick Posted January 19, 2015 Share Posted January 19, 2015 Intended as far as I know. I usually run into this more with mouse hit so I generally create a bool value to indicate if it's down or not and use MouseDown() with. Probably easier with mouse vs keys though since I generally only look at left/right buttons. if MouseDown(1) == true && leftDown == false then -- left mouse click and will only come in here once per click leftDown = true else if MouseDown(1) == false and leftDown = true then -- left mouse released so reset leftDown = false end Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 19, 2015 Author Share Posted January 19, 2015 Thanks for the answer everyone. I find it quite odd why I shouldn't be able to check this multiple times per frame. That boolean for the key being pressed is set and not being flushed out. I can walk my way around it like you guys are suggesting, but having all of these extra booleans for every key and mousehit seems quite messy. Quote Link to comment Share on other sites More sharing options...
Averice Posted January 21, 2015 Share Posted January 21, 2015 I wrote a quick event binding module in lua for the game I'm working on, all you have to do is import this file and add event.CheckInput() in your loop somewhere. This solves the problem of leaving unchecked keys in the event and also allows you to add more functionality to a single key check instead of adding it all in one massive loop in one file. http://pastebin.com/yQTC6BQP For some reason I can't paste the code here because it won't copy tabs and looks ugly Usage is quiet simple. -- keyboard binds. event.AddInputBind(key, name, held, func, ...) -- Key code, Unique Name, Run on key held?, function, function args event.AddInputBind(Key.A, "unique_name_here", true, print, "hello", "leadwerks", "community"); -- mouse binds. event.AddMouseBind(but, name, func, ...) -- mouse button code, unique name, function, function args. event.AddMouseBind(mousebuttoncode, "unique name", print, "hey there"); I have quite a few helpful modules I've written for the game I'm working on, is there somewhere you guys would like me to put them so others can take advantage of them? 1 Quote Link to comment Share on other sites More sharing options...
Rick Posted January 21, 2015 Share Posted January 21, 2015 Nice code, mind if I add it to the wiki? Quote Link to comment Share on other sites More sharing options...
Averice Posted January 21, 2015 Share Posted January 21, 2015 Nice code, mind if I add it to the wiki? Go right ahead, would be good for others to get some use from it too. Alright I've decided to post some modules on my leadwerks.com blog, when I get spare time I'll try to post more of them with some usage guides. http://www.leadwerks.com/werkspace/blog/154-lua-is-better-than-you-think/ 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.