How it works is the user hooks into a pre-created event, hooking is done so :
Assume we have function "printKey", it prints its passed arguments out, [key] and [state] respectively, when called,
Now we hook it to OnkeyAction which is fired every key press/release and passes the arguments [key] and [state] to any functions hooked on it.
/
Love.keypressed/released() call the event passing its arguments onto the event ie keypressed(key) ---> OnKeyAction(key,state) where state is set by whether its keyPRESSED or keyRELEASED, true and false respectively
/
How do i add the hooks? simply taking a table and storing the function inside!
ie :
Code: Select all
function printKey(key,state)
print(key .. " : " .. tostring(state))
end
function addHook(eventName,func)
if EventsDB[eventName] then
table.insert(EventsDB[eventName],func)
end
end
function love.keypressed(key)
FireEvent("OnKeyAction",true)
--false if was keyreleased
end
function FireEvent(name,...)
if EventsDB[name] then
for k,v in pairs(EventsDB[name]) do
v(...) -- calls the function, passing the additional arguments on
end
end
end
--register the hook
addHook("OnKeyAction",printKey)
The issue stands with :
The "..." argument isnt passing ALL the arguments it should in FireEvent(),
Now i understand that in functions using the '...' in another function or operation inside themselves (like v(...)) makes it use the last/first* argument...
*unsure.
BUT earlier i made a shape Setter function like :
function setShape(func,...)
love.physics.func("fill",...)
end
in another function i check some data, then call the setShape passing a function and table,
The function passed is either love.physics.newPolygonShape or love.physics.newRectangleShape
and the shape setter doesnt complain at all...
SO my question is, is there some way i can fix this? as i need to pass the additional arguments an event fire uses such as [key] and [state]
yet atm i can seem to get it to be happy as doing even a
Code: Select all
function FireEvent(name,...)
if EventsDB[name] then
args = {...}
for k,v in pairs(EventsDB[name]) do
v(args) -- calls the function, passing the additional arguments on
end
end
end
--FAILS, results in same issue
---as there is also this method:
function FireEvent(...)
args = {...} -- still fine, all arguments kept
name = args[1] -- STILL fine
local targs = {}
for i = 2,#args do
table.insert(targs,args[i])
end
---Still fine!
if EventsDB[name] then
for k,v in pairs(EventsDB[name]) do
v(targs) -- calls the function, passing the additional arguments on
end
end
end
--Fails in the actual hooked function, printing the single variable [targ] would be yields a nil!
even if its not a answer, advice in this system is also welcome!