Lua sending a called function's name - lua

Basically I want to know when function;
button[n]:onclick() --where n can be any string value
gets called, and send its name (specifically I want to send "n") to another function;
function allbuttons(n) --where n is the n from button[n]:onclick()
which then processes all possible requests.
I want to do this because button[n] is specified, and therefore the button[n]:onclick() gets triggered every time the button is clicked, but it doesn't seem right to write these function every time I want another buttonclick to be processed in the big allbuttons function;
function button['options']:onclick()
allbuttons('options')
end
function button['quit']:onclick()
allbuttons('quit')
end
(...)
function button[n]:onclick()
allbuttons(n)
end
I've tried something like;
debug.sethook(allbuttons, 'c')
function allbuttons()
n = debug.getinfo(2).name
end
but I guess I don't fully understand how to use debug.sethook ..

Set button[n]:onclick to be the function you want (allbuttons), except that here there is one tricky bit, the value n. You likely know already that you could do
button[n].onclick = allbuttons
But if the event dispatcher calls onclick as button[n]:onclick() then allbuttons will always get button as first argument. If what you really want in allbuttons is to know the button[n] instance that was clicked, all you need to do is change the definition of allbuttons(n) to allbuttons(button) and change its code accordingly.
If you need n and it's not available any other way, you can create an anonymous closure with access to n as an upvalue (see http://www.lua.org/pil/6.1.html for details):
function sendClickToCommon(button, n)
button[n].onclick = function (self)
allbuttons(n)
end
end
sendClickToCommon(button, 1)
sendClickToCommon(button, 2)
sendClickToCommon(button, 3)
Or you could do it this way too:
function getAllbuttonsN(n)
return function (self)
allbuttons(n)
end
end
button[1].onclick = getAllbuttonsN(1)
The code is simpler but the index appears twice in the expression, a potential source of error.

Related

how is a function navigating through fields of a table

i already asked this question on stackoverflow and accepted an answer i think i did not really understand the answer and i got some more question, im embarrassed to necro it so im creating a new question
im learning lua and got to metatable part, in this example
local tb = {}
local meta = {}
function tb.new(s)
local super = {}
super.s = s
setmetatable(super,meta)
return super
end
function tb.add(s1,s2)
return s1.s..s2.s
end
meta.__add = tb.add
f= tb.new("W")
t= tb.new("E")
print(f+t)
when compiler gets tof = tb.new("W")
i think this happens
function tb.new("W") super.W = W return setmetatable(super,meta) return super end
so
print(f+t)
looks like
print(super+super)
how does
tb.add(super,super)
find the fields of super table using
return s1.s..s2.s
also as the
tb.new
function is called twice and
setmetatable(super,meta)
happens twice is there any difference between the first and second iteration? if any of the above are incorrect please correct me.
when compiler gets tof = tb.new("W") i think this happens function tb.new("W") super.W = W return setmetatable(super,meta) return super end
No. It's more like super['s'] = 'W'. That's how dot notation works. I hope that clarifies how Lua "finds the fields" later.
as the tb.new function is called twice and setmetatable(super,meta) happens twice is there any difference between the first and second iteration?
They're different, because the super variable is a new table each time. Any time you see {} (a table constructor), whether empty or not, it's creating an entirely new table. meta, however, is still the same table, because it only gets a table constructor once, outside of any function.

Iup Lua callback -- what am I doing wrong?

I'm using Lua 5.1 with IUP 3.5, and trying to use a list callback to populate an Address list depending on the Place selected. (The list is an editbox, so I will need to handle that in due course, but let us deal with the basics first). I've clearly got a fundamental misunderstanding of how to do this.
The code:
function MakeAnIupBox
--make some more elements here
listPlace = iup.list{}
listPlace.sort = "YES"
listPlace.dropdown = "YES"
--populate the list here
--now handle callbacks
listPlace.action = function(self) PlaceAction(text, item, state) end
end
function PlaceAction(text, item, state)
listAddress.REMOVEITEM = "ALL"
if state == 1 then -- a place has been selected
--code here to populate the Addresses list
end
end
The iup documentation describes the action callback for a list as
ih:action(text: string, item, state: number) -> (ret: number) [in Lua]
However, when I run this code I get:
text -- looks like some sort of metatable
item, state -- both nil
I've also tried coding the callback as
function MakeAnIupBox
--make some more elements here
listPlace = iup.list{}
listPlace.sort = "YES"
listPlace.dropdown = "YES"
--populate the list here
end
function listPlace:action (text, item, state)
listAddress.REMOVEITEM = "ALL"
if state == 1 then -- a place has been selected
--code here to populate the Addresses list
end
end
but that fails to run: the error is attempt to index global 'listPlace' (a nil value)
I'd prefer not to embed the callback in "MakeAnIupBox" because I'm hoping to make it (and the other associated callbacks) a resuable component in several Lua programmes that all process similar datasets but from different UIs.
If you do not want to embed the callback function inside your function, you can define it before and later assign it to your specified destination.
function Callback(self, a, b)
-- do your work ...
end
function CallbackUser1()
targetTable = { }
targetTable.entry = Callback
end
function CallbackUser2()
otherTargetTable = { }
otherTargetTable.entry = Callback
end
This solution needs the arguments to be always the same.
Note: All following definitions are identical
function Table:func(a, b) ... end
function Table.func(self, a, b) ... end
Table.func = function(self, a, b) ... end
The problem is in Lua usage.
In the first case, remember that this:
function ih:action(text, item, state)
translates into this:
function action(ih, text, item, state)
So it is missing the ih parameter.
In the second case, listCase exists only after MakeAnIupBox is called. You can solve that by declaring the function inside the MakeAnIupBox scope.
Building on the suggestion of Antonio Scuri which was not totally explicit, I have worked out that the code needs to read:
function MakeAnIupBox
--make some more elements here
listPlace = iup.list{}
listPlace.sort = "YES"
listPlace.dropdown = "YES"
--populate the list here
--now handle callbacks
listPlace.action = function(self, text, item, state) PlaceAction(listPlace, text, item, state) end
end
function PlaceAction(ih, text, item, state)
listAddress.REMOVEITEM = "ALL"
if state == 1 then -- a place has been selected
--code here to populate the Addresses list
end
end

Lua is it possible to "halt" any code execution from within a table?

Something that intrigues me about Lua is the fact that you can run any function from within a table, regardless of whether it returns anything or not, an example of what I am talking about is:
local my_table = {
print("output from a table!");
warn("more output from a table!");
};
The funny thing is that as soon as this table is created both functions inside of it are ran, and both my_table[1] and [2] are equal to nil (because print and warn do not return a value). However is there any way to per-say "halt" both functions from executing whenever the table is created, and possibly even "start" running them later, if a certain condition is met or not?
I would appreciate any help; Thanks
You're not storing functions in a table that way, you're storing results of calls.
If you need functions, create anonymous functions explicitly.
local mytable = {
function() print("output from a table!") end,
function() warn("more output from a table!") end
}
If you don't like this way, there's another. Capture function and arguments in a lexical closure, and apply stored arguments when that closure will be called.
local function delay(func, ...)
local args = {...}
return function()
func(table.unpack(args))
end
end
local mytable = {
delay(print, "output from a table!"),
delay(warn, "more output from a table!")
}
mytable[1]()
mytable[2]()

List of functions

Following definitions print already the result of the functions in the table:
function plus1(zahl) print(zahl+1) end
function plus2(zahl) print(zahl+2) end
function plus3(zahl) print(zahl+3) end
-- already prints out 6,5,10
local tfunc={plus1(5),plus2(3),plus3(7)}
how can I avoid this?
how can I iterate through the functions with given parameters in the table? I would like to call the functions like:
tfunc[1]
to print out 6. But it does not work.
#pschulz, thanks to show me the way :
local tfunc = {{plus1,5},{plus2,3},{plus3,7}}
tfunc[i][1](tfunc[i][2])
allows to iterate with index i through the different functions with different args. The trick is tables with function name and args inside the table.
In you table, you are currently storing nothing (or three nil). The table takes the return value of the function and since you are returning nothing, it gets nil.
What you have to do is store functions:
local tfunc = {
plus1,
plus2,
plus3
}
No you can call your functions like this:
tfunc[1](5)
On iterating: If i understand correctly, you want to do the following:
local tfunc = {
plus1,
plus2,
plus3
}
local tvalues = { 5, 3, 7 }
for i, func in ipairs(tfunc) do
func(tvalues[i])
end
So you have to save your values in another table. There are more elegant ways to do this, i suggest you have a reading on closures.
local tfunc={plus1(5),plus2(3),plus3(7)}
Here you call the functions inside the table constructor.
So once you create your table you will print those values. As your function does not return anything tfunc remains empty.
You can either store the function call as a string and let Lua execute that string or you can save the function in the table without calling it. Then you need some way to get the function parameter zahl into your function call.
To me what you want to do makes no sense.
If you want to call it like tfunc[1] it will always print 6. So why not just call print(6)?
If you want to add 3 to a number you want to print, just call print(number + 3) e.g.
Your code will remain more readable and easier to understand if you don't move simple arrithmetics into an extra function.

How to prevent Lua from returning the final value of an expression?

This is a weird question as I'm not so sure how to ask, but here's the problem:
BeforeTime = os.clock()
function NewFunction( Name, Value )
Name = function()
return Value
end
end
NewFunction( RunningTime, (os.clock()-BeforeTime) )
while true do
print(RunningTime()) -- will always return same value, i want the updated
end
The example above is not exactly my context, but its the easiest way to explain my problem.
I guess I could require that the parameter 'Value' needs to be a function, but is there another way?
NewFunction creates a function and assigns it to the Name parameter. But, in Lua, actual parameters are passed by value (like Java and C) and formal parameters are effectively local variables. The assigned value is never used and it can't be used outside the function.
To make a function return a non-empty list of values, use a return statement.
Here's a similar function for a timer:
local function CreateTimer()
local BeforeTime = os.clock()
return function()
return os.clock() - BeforeTime
end
end
You can use it like this:
local RunningTime = CreateTimer()
while true do
print(RunningTime())
end
Your code does not run because NewFunction doesn't do what you think it should.
If you add the line below before the loop, then it works fine.
RunningTime = function() return (os.clock()-BeforeTime) end

Resources