Jump to content

mouse/keyboard states in multiple scripts


AggrorJorn
 Share

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

  • Upvote 1
Link to comment
Share on other sites

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.

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.

 Share

×
×
  • Create New...