passing additional arguments to a function using '...' ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

passing additional arguments to a function using '...' ?

Post by ArchAngel075 »

Right, so ive been struggling for 2 so hours now to get a "event" system going for my game....

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)
NOW THEN!
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!
Im at a lot with this.. so any advice is greatly appreciated!
even if its not a answer, advice in this system is also welcome!
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: passing additional arguments to a function using '...' ?

Post by miko »

ArchAngel075 wrote:Im at a lot with this.. so any advice is greatly appreciated!
Please could you attach a *.love file demonstrating what does not work for you? Because it does work for me (see attachment), or I do not understand your problem correctly.
Attachments
events.love
(494 Bytes) Downloaded 53 times
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 190 guests