Jump to content
Phodex Games

KeyHit/MouseHit multiple scripts solution?

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.

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post

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.

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