upvalues not working

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
MoseGames
Prole
Posts: 3
Joined: Tue Feb 21, 2017 2:43 am

upvalues not working

Post by MoseGames »

ret = function()
return x
end

debug.setupvalue(ret , 1 , {x = 90})

love.errhand( ret() )

shows nil when it should return 90
this workes in the lua console on mac.
thanks
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: upvalues not working

Post by s-ol »

MoseGames wrote: Tue Feb 21, 2017 12:37 pm ret = function()
return x
end

debug.setupvalue(ret , 1 , {x = 90})

love.errhand( ret() )

shows nil when it should return 90
this workes in the lua console on mac.
thanks
In your code `x` is not an upvalue, it's a global variable. Also you are using `debug.setupvalue` wrong. If you want to return 90 it should be debug.setupvalue(ret, 1, 90).

If x is supposed to be an upvalue it needs to be local and bound to another scope, here's a working example:

Code: Select all

function makeClosure()
  local x
  return function()
    return x
  end
end

ret = makeClosure()

print(ret()) -- prints nil

debug.setupvalue(ret, 1, 90)

print(ret()) -- prints 90

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: upvalues not working

Post by zorg »

That was also about the same answer you got on the issue tracker. :3
(Also, i'm pretty sure the line love.errhand( ret() ) doesn't work on the (plain) lua console itself, since it's something that löve defines...)
Nice website, by the way.
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.
MoseGames
Prole
Posts: 3
Joined: Tue Feb 21, 2017 2:43 am

Re: upvalues not working

Post by MoseGames »

Thank you.
ps. I used print in console not love.errhand()
Post Reply

Who is online

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