Upvalue is a mechanism which is equivalent of C static variable. An upvalue is visible only inside the function in which is it declared.
Questions tagged [upvalue]
10 questions
16
votes
2 answers
What is the meaning of 'attempt to index upvalue'
I am taking my first steps programming in Lua and get this error when I run my script:
attempt to index upvalue 'base' (a function value)
It's probably due to something very basic that I haven't grasped yet, but I can't find any good information…

Cactuar
- 395
- 2
- 6
- 14
3
votes
1 answer
Lua: Access the for loop variable from outside of the loop
I would like to use a for-loop in Lua, but be able to identify what the last iterated value was:
local i
for i=0,10 do
if i==5 then break end
end
print(i) --always prints nil
Is there some way to prevent "i" from being re-declared in the for…

Star Brood
- 31
- 4
3
votes
2 answers
change/update value of a local variable (Lua upvalue)
I've written a script to hot-reload already requireed modules. It work s only partially however...
My approach to this task is quite simple. I changed Lua's require function so that it remembers modules that it loaded together with a timestamp and…

geekhunger
- 470
- 5
- 9
2
votes
1 answer
how to use the debug library to interact with a required lua file's upvalues
If I have a Lua function f I can look at all the upvalues in f's closure by using the debug.getupvalue function. Similarly if I have a file foo.lua I can look at the upvalues by first doing foo = loadfile(foo) then using debug.getupvalue in the same…

Ace shinigami
- 1,374
- 2
- 12
- 25
2
votes
1 answer
What are shared upvalues?
How do two closures share an upvalue? And how does it work?
function print_env()
print(_ENV) --_ENV is an upvalue
end
function foo()
_ENV = { print = print, print_env = print_env} --redefine the _ENV upvalue
print(_ENV) --prints: 0094CF40
…

Tiago Costa
- 4,151
- 12
- 36
- 54
1
vote
2 answers
Lua upvalues not behaving as expected with local variables
Consider this Lua 5.1 code:
function foo ()
function makeAdder (withWhat)
return function (a)
return a + withWhat
end
end -- makeAdder
f1 = makeAdder (6)
f2 = makeAdder (7)
end -- for
foo ()
print (f1 (2)) --> 8
print (f2 (2)) -->…

Nick Gammon
- 1,173
- 10
- 22
1
vote
0 answers
Why can the parameter `n` of function l`ua_upvalueindex` be 256?
Hi~ I need some helps as i couldn't understand c closures in lua reference manual. As follows:
2)
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
...
The maximum value for n is 255.
...
1)
Any access to lua_upvalueindex(n),…

Licai Liu
- 11
- 3
1
vote
2 answers
Attempt to index upvalue
I have just started learning Lua today. I have been doing the tutorials on coronalabs.com website... I tried to adapt a the first exercise with tapping the bouncing balloon into the scenes template of the asteroids games. Can someone tell me how I…

Atrag
- 653
- 2
- 8
- 29
0
votes
0 answers
Lua up-value issue for function in table
I am trying to create parts of tables many times but with different arguments. To do so, I have created a new function that takes a parameter, which is then used in a function in the table. My IDE shows the use of the parameter value inside the…

Christopher Kas
- 31
- 4
0
votes
1 answer
Attempt to call upvalue 'Message' (a table value)
So, I'm working on Discord API wrapper written in Lua, but there's an issue, I use rxi's classic module for creating classes and objects, every class works fine, instead of the one class, Message, while trying to create a message in Interaction…

HazardousDev
- 9
- 9