Page 1 of 1

upvalues not working

Posted: Tue Feb 21, 2017 12:37 pm
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

Re: upvalues not working

Posted: Tue Feb 21, 2017 1:21 pm
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

Re: upvalues not working

Posted: Tue Feb 21, 2017 1:23 pm
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.

Re: upvalues not working

Posted: Tue Feb 21, 2017 9:36 pm
by MoseGames
Thank you.
ps. I used print in console not love.errhand()