Jump to content
AggrorJorn

mouse/keyboard states in multiple scripts

Recommended Posts

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?

Share this post


Link to post

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.

Share this post


Link to post

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".

Share this post


Link to post

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

Share this post


Link to post

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.

Share this post


Link to post

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?

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...