table in love.event.push will lose metatable

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
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

table in love.event.push will lose metatable

Post by abyssol »

i am using lua oop to program, and i wanna pass an object as the parameter of callback functions to know which object pushed this event, but passed object lost its metatable in callback function, i can only access its data like o.text, no o:getText()

i am trying to use love.event instead of rewriting a new event system, so i have to write an own event sys?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: table in love.event.push will lose metatable

Post by MrFariator »

Can you share some code, to illustrate the problem you are having? The part about losing the metatable in the process sounds odd.

Do remember that the colon(:) syntax is just syntactical sugar:

Code: Select all

myObject:myFunction(arg1)
-- is the same as
myObject.myFunction(myObject,arg1)

function myObject:myFunction ( arg1 ) -- "self" is inferred
  -- ...function body goes here
end
-- is the same as
function myObject.myFunction ( self, arg1 ) -- "self" is just convention here, if you're passing along objects
  -- ...function body goes here
end
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

Re: table in love.event.push will lose metatable

Post by abyssol »

MrFariator wrote: Tue Nov 28, 2023 10:44 pm Can you share some code, to illustrate the problem you are having? The part about losing the metatable in the process sounds odd.

Code: Select all

local o = Human()
love.graphics.print(o:getName()) -- fine
love.event.push('MOVED', o)
-- or a wrapped method o:push('MOVED')
function handleMoved(e)
  if e:getName() -- error
  if e.name -- fine
end
arg 'o' has no metatable in callback function that handles 'MOVED' event.

I think i have to write an own event.push to pass a true reference.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: table in love.event.push will lose metatable

Post by zorg »

you should not use löve's event mechanics like that; yes, you should implement your own.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 62 guests