Jump to content

KeyHit/MouseHit multiple scripts solution?


Phodex Games
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

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