Phodex Games Posted March 2, 2017 Share Posted March 2, 2017 Hi guys, I searched the forum a bit and found out that other people had the same issue as I have now. You maybe know that if you have a key check (like window:KeyHit() or window:MouseHit()) in multiple scripts then only ONE of the works as expected. I read that its because Leadwerks sets the hit check value to false right after you checked it. However I tried to find a solution for this, by making a "class" where I call all keychecks, unfortunately the problem remains, maybe you can help me? What have you come up with? Thats my attempt: Thats the class script: function KeyCheck(script) local key = {} function key:MouseClicked(command) if command == "Left" then return window:MouseHit(1) elseif command == "Right" then return window:MouseHit(2) end end return key end These are two test scripts: import "Scripts/+Morra Scripts/System/KeyCheck.lua" local keyCheck function Script:Start() self.loadClass = KeyCheck keyCheck = self:loadClass() end function Script:UpdateWorld() if keyCheck:MouseClicked("Left") then System:Print("Test1") end end import "Scripts/+Morra Scripts/System/KeyCheck.lua" local keyCheck function Script:Start() self.loadClass = KeyCheck keyCheck = self:loadClass() end function Script:UpdateWorld() if keyCheck:MouseClicked("Left") then System:Print("Test2") end end I only get printed "Test1", due to this script seems to be called faster than the second one. Quote Link to comment Share on other sites More sharing options...
Genebris Posted March 2, 2017 Share Posted March 2, 2017 Why don't you create a table for buttons you need, check them once per update and use this table in every script? Your class can't help you because you are still calling MouseHit multiple times per frame. You shouldn't do that. 1 Quote Link to comment Share on other sites More sharing options...
Thirsty Panther Posted March 2, 2017 Share Posted March 2, 2017 I think Aggror had a solution to this problem. Search the forums 1 Quote Link to comment Share on other sites More sharing options...
Rick Posted March 2, 2017 Share Posted March 2, 2017 You could use an event system. I have a small event class. Your KeyCheck could be a global table that has onKeyHit event. Then inside the scripts you need you include the table like you're doing and you bind that scripts function you want to have called when onKeyHit is raised. The event code allows binding multiple functions from any object. Then your KeyCheck would have an update function that looks for key hits and raises the event which in turn calls all the script functions you bound to it. Then call that KeyCheck:Update function in the main.lua files main loop. The benefit is that you aren't polling in each script so it'll be faster as the more scripts you add what your currently doing the more checking that happens. It also only ever calls the hit functions once then to solve your current issue. Kills two birds with one stone! Usage is inside your table that handles key presses: self.onKeyHit = EventManager:Create(self) To raise event self.onKeyHit:Raise({ key = key}) if EventManager ~= nil then return end EventManager = {} function EventManager:Create(owner) local obj = {} obj.handlers = {} obj.owner = owner for k, v in pairs(EventManager) do obj[k] = v end return obj end function EventManager:Subscribe(owner, method) table.insert(self.handlers, { owner = owner, method = method }) end function EventManager:Raise(args) for i = 1, #self.handlers do self.handlers.method(self.handlers.owner, self.owner, args) end end 1 Quote Link to comment Share on other sites More sharing options...
Thirsty Panther Posted March 3, 2017 Share Posted March 3, 2017 Here is Aggrors query and Averice solution. http://www.leadwerks.com/werkspace/topic/11709-mousekeyboard-states-in-multiple-scripts/page__hl__mousehit#entry84590 And his Blog of the same topic. http://www.leadwerks.com/werkspace/blog/154/entry-1406-keyboard-and-mouse-events/ 1 Quote Link to comment Share on other sites More sharing options...
Phodex Games Posted March 3, 2017 Author Share Posted March 3, 2017 Thanks to all of you for your help I gues that should get the job done. I really appreciate your numerous suggestions . I am sorry if this topic is solved already, but I couldn't find anything useful in the forum. 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.