function checkBox(x1,y1,x2,y2, func)
f = {fun=func}
e,b,x,y = os.pullEvent("mouse_click")
for a = 1, y2-y1+1 do
for b = 1, x2-x1+1 do
if a == x and b == y then
f[fun]()
end
end
end
end
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Call A Function Depending On A Variable
Started by jay5476, 17 August 2013 - 04:40 AMPosted 17 August 2013 - 06:40 AM
how do I call a function depending on a variable this is my code that does not work, how would I implement a way to run the function determined by func
Posted 17 August 2013 - 06:53 AM
f[fun] means fun is interpreted as a variable in that context, i.e. Lua tries to look up a variable with that name, which doesn't exist, so it's the same as f[nil], which is, in turn, nil. What you actually want to do is f.fun, or the equivalent f["fun"].
Unless fun actually is a global variable you didn't show here, in which case you want to change the initialization of your table to f = {[fun] = func}.
Unless fun actually is a global variable you didn't show here, in which case you want to change the initialization of your table to f = {[fun] = func}.
Posted 17 August 2013 - 07:02 AM
Sounds like fun. ctions
Posted 17 August 2013 - 08:41 AM
thanks guy i ended up sorting this out in the meantime but your replays are still appreciated
Posted 17 August 2013 - 09:55 AM
Remove "f = {fun=func}" and change "f[fun]()" to "func()". No special code needed - you can pass functions around just like you can pass numbers and strings and tables around.
Posted 17 August 2013 - 06:28 PM
What in the [..]Sounds bolean to me
anyway, passing Around variables, which is a boolean, string, number, table and functions, is never hard.
However if you talk about tables, you are possibly conflicting with pointers. It basically does not copy the table, as opposed to the other types of a variable. A pointer points to a place in the memory, so if you change the pointer you change the actual table.
And on a side not for the OP, you can declare table functions like so:
local tbl = {
['key'] = function( args )
print( tostring(args))
end;
}
local t = {}
function t.key( args )
print( tostring(args))
end
Posted 17 August 2013 - 06:41 PM
You are wrongf[fun] means fun is interpreted as a variable in that context, i.e. Lua tries to look up a variable with that name, which doesn't exist, so it's the same as f[nil], which is, in turn, nil. What you actually want to do is f.fun, or the equivalent f["fun"].
Unless fun actually is a global variable you didn't show here, in which case you want to change the initialization of your table to f = {[fun] = func}.
this expression makes a table with fun in it. and fun is set as a pointer to func
f = {fun = func}
Posted 17 August 2013 - 06:57 PM
You are wrong
this expression makes a table with fun in it. and fun is set as a pointer to func
f = {fun = func}
I am most certainly not wrong ;)/> I am aware that OP's table initialization code does that, which is why I'm stating how to properly access it in the first paragraph of my post.
The second paragraph is just there for completion's sake: given a hypothetical global variable fun not mentioned in OP's code actually existed then f = {[fun] = func} … f[fun] would work perfectly well, too - since the table entry's name would be the value of the variable fun.
Also: what the hell happened to this thread? o.O
Posted 17 August 2013 - 07:29 PM
You are wrong
this expression makes a table with fun in it. and fun is set as a pointer to func
f = {fun = func}
I am most certainly not wrong ;)/> I am aware that OP's table initialization code does that, which is why I'm stating how to properly access it in the first paragraph of my post.
The second paragraph is just there for completion's sake: given a hypothetical global variable fun not mentioned in OP's code actually existed then f = {[fun] = func} … f[fun] would work perfectly well, too - since the table entry's name would be the value of the variable fun.
Also: what the hell happened to this thread? o.O
I am not sure what your point is, but if a global variable name fun existed it would not be accessable for f[fun]
I.e:
local fun = function() print("I am 'global' fun") end
local f = {fun = function() print("I am a child of f") end}
f.fun() --prints: I am a child of f
f["fun"]() --prints: I am a child of f
fun() --prints: I am 'global' fun
I do not what happened to the thread xD(random junk)
Posted 17 August 2013 - 07:43 PM
I've cleaned up the thread some. Guys, please keep things related to the question on hand when you're in AaP. I'm not trying to be a dictator here, I just want don't want people to have to sift through random crap when they're looking for answers.
Thanks!
Thanks!
Posted 17 August 2013 - 07:47 PM
Jarle212: Ah, I see where our misunderstanding lies, I think. I'm not saying fun is the actual function. It can be anything at all, just not nil. In which case, using your example:
local fun = {some = "variable", ["let's make it a table for"] = "fun"}
local f = {[fun] = function() print("blah") end}
f[fun]() -- prints: blah