I'm trying to iterate over a table of functions, and call each function one by one. I keep getting the 'attempt to access a field/method NIL value. Some of them are obvious, but some of them baffle me.
tests={}
function tests.test1()
print("test1 running\n")
end
function tests.test2()
print("test2 running\n")
end
function tests.test3()
print("test3 running\n")
end
print(tests)
-- why is tests.i() or tests:i not working?
for i,x in pairs(tests) do
print("x is")
print(x)
print("i is")
print(i)
--tests.i() -- attempt to call field i a NIL value
--tests.i(tests) -- ditto
--tests:i() -- attempt to call method i, a NIl value
--tests:i(tests) -- ditto
tests[i]() -- works!
--tests.x() -- attempt to call field x, a NIl value
--tests.x(tests) -- ditto
--tests:x() -- attempt to call method x, a NIL value
--tests[x]() -- attempt to call field ? a NIl value
end
--all these work
tests.test1(tests)
tests:test1()
tests.test2(tests)
tests:test2()
tests.test3(tests)
tests:test3()
Why is the tests.i() method not working, or tests.i(tests), if it's expecting a 'self' argument, in which case why isn't tests:i() working? The last bit shows all of them working when called outside the loop, which makes it even more difficult to understand.