So I'm currently working on creating blocks of codes which can be called simultaneously by a name id. I've decided to do that with a main table, which contains table with id and with functions. To do that, I wrote 3 functions
function hook.add(name, hookname, func)
hooks[hookname[name]] = func
end
function hook.create(name)
hooks[name] = {}
end
function hook.run(name)
for _, func in pairs(hooks[name]) do
func()
end
end
hook.create("MainHook")
local function func()
print("working")
end
hook.add("todo", "MainHook", func)
However it doesnt work and crashes with
bin/hooks.lua:27: table index is nil
Error contains in
hooks[hookname[name]] = func
line but I have no idea why because even if i print hookname and name there is no nil at all.
I would really appreciate if you help me
Your function hook.create creates empty table for name, so function hook.add should look like this:
function hook.add(name, hookname, func)
-- create hooks[hookname] table if not exists
hooks[hookname] = hooks[hookname] or {}
-- add function to hooks[hookname] table
hooks[hookname][name] = func
end
Related
tablelogin = {0 = "test1",1 = "test2",2 = "test3",3 = "test4"}
for pp=0,#table do
if takeinternalogin == (tablelogin[pp]) then
LogPrint("Login found")
else
LogPrint("failed login not found")
end
end
takeinternalogin is an internal function of my program that takes the person's login.
In this script I'm taking the person's login and comparing whether or not the login is in the table.
It works, but after the else if the person's login is not in the table, it returns the message "failed login not found" 4 times, that is, it returns the number of times it checks the table.
I don't understand. How can I make the message execute only 1 time?
first of all table is Lua's table library. You should not use it as a variable name. Unless you've added numeric fields to that table or replaced it with something else #table should be 0 and hence your loop should not do anything.
But as you say your loop runs 4 times I suppose you modified table.
You say internallogin is a function so you can never enter the if block as you compare a function value cannot equal a string value: takeinternalogin == (tablelogin[pp] is always false! takeinternallogin would have to return a string.
Why you're using #table here in the first place is unclear to me.
You are currently printing the error message every time it iterates over the table and the current value does not match.
local arr = {[0] = "test1", [1] = "test2", [2] = "test3", [3] = "test4"}
function findLogin(input)
for i,v in pairs(tablelogin) do
if v == input then
LogPrint("Login found")
return i
end
end
LogPrint("failed login not found")
end
login = findLogin(takeinternalogin)
Using return within a loop makes it break out of the loop and in this case never reach the line where it prints the error.
Is it possible to have a function that can access arbitrarily nested entries of a table?
The following example is just for one table. But in my real application I need the function to check several different tables for the given (nested) index.
local table1 = {
value1 = "test1",
subtable1 = {
subvalue1 = "subvalue1",
},
}
local function myAccess(index)
return table1[index]
end
-- This is fine:
print (myAccess("value1"))
-- But how do I access subtable1.subvalue1?
print (myAccess("subtable1.subvalue1???"))
You won't be able to do this using a string unless you use load to treat it as Lua code or make a function to walk on a table.
You can make a function which will split your string by . to get each key and then go one by one.
You can do this using gmatch + one local above gmatch with current table.
#Spar: Is this what you were suggesting? It works anyway, so thanks!
local table1 = {
value1 = "test1",
subtable1 = {
subvalue1 = "subvalue1",
},
}
local function myAccess(index)
local returnValue = table1
for key in string.gmatch(index, "[^.]+") do
if returnValue[key] then
returnValue = returnValue[key]
else
return nil
end
end
return returnValue
end
-- This is fine:
print (myAccess("value1"))
-- So is this:
print (myAccess("subtable1.subvalue1"))
I have an empty table which I want to act as a "gateway" to another set of functions at another location.
tbl = {}
I want to pass called functions from this table to somewhere else as a string:
tbl.someMethod("hello")
I've tried this with limited success.
hand = {
__index = function(tbl, name)
hand[name] = function(...)
passToSomewhere(name, ...)
end
end,
__call = function(tbl, name, ...)
hand[name](...)
end
}
setmetatable(tbl, hand)
tbl.someFunction("hello!", someTbl, someNumber)
How do I forward the undefined function through the table without it throwing errors?
Edit: More detail
I'm trying to define and call a function in a table in one call:
tbl = {}
hand = {
__index = function(tbl, name)
print(name)
tbl[name] = function(...)
print(...)
end
end
}
setmetatable(tbl, hand)
s,e = pcall(tbl.help,"banana","goat")
print(s)
s,e = pcall(tbl.help,"banana","goat")
print(s)
This code does work but the first pcall will throw an error because the function hasn't been defined yet.
Say I wanted to use an library which I know updates quite a lot and keep my script compatible and this library may not be present on my computer. I would like to forward calls to this library across some interface but I still want to be able to call the functions in the same way.
--For example I would like to call this function like this:
someLib.doSomething(name, age, telephone)
--Instead of passing it through another function:
someOtherLib.invoke("someLib.doSomething", name, age, telephone)
Is this possible?
Edit 2:
Thanks #greatwolf !
This is my working test code.
tbl = {}
hand = {
__index = function(tbl, name)
tbl[name] = function(...)
return print(name, ...)
end
return rawget(tbl, name)
end
}
setmetatable(tbl, hand)
tbl.help("banana","goat")
Okay, based on your updated details you want lua to translate this call
someLib.doSomething(name, age, telephone)
into
someOtherLib.invoke("someLib.doSomething", name, age, telephone)
behind the scenes. What you have is almost there, you just need to return the newly created function back:
__index = function(tbl, name)
tbl[name] = function(...)
return someOtherLib.invoke("someLib."..name, ...)
end
-- return tbl[name] works too, I used rawget to indicate
-- no further __index lookup should be done
return rawget(tbl, name)
end
Now, if your someOtherLib is just a table of functions, lhf's suggestion will work too
setmetatable(tbl, {__index = someOtherLib})
Now if your someOtherLib provides someway to get the function you want to call without actually invoking it just yet, __index can relay this without creating extra closure wrappers
__index = function(tbl, name)
tbl[name] = someOtherLib.getFuncByName(name)
return tbl[name]
end
The __call metamethod is not needed here.
I'd like to create a simple mock table that would tell me what was tried to be called from it.
My first try was:
local function capture(table, key)
print("call to " .. tostring(table) .. " with key " .. tostring(key))
return key
end
function getMock()
mock = {}
mt = { __index = capture }
setmetatable(mock, mt)
return mock
end
Now calling this with
t = getMock()
t.foo
prints as I expected:
call to table: 002BB188 with key foo
but trying to call:
t.foo("bar")
gives:
call to table: 002BB188 with key foo
lua: test.lua:6: attempt to call field 'foo' (a string value)
Now I have two questions:
How to avoid the exception, ie. what am I doing wrong?
How to catch the method argument too ("bar" in this case)?
You need to return a function from the __index handler, not a string:
local function capture(table, key, rest)
return function(...)
local args = {...}
print(string.format("call to %s with key %s and arg[1] %s",
tostring(table), tostring(key),
tostring(args[1])))
end
end
-- call to table: 0x7fef5b40e310 with key foo and arg[1] nil
-- call to table: 0x7fef5b40e310 with key foo and arg[1] bar
You're getting an error because it's trying to call the result, but it's currently the key.
In lua ,im calling a function which returns a table variable that contains many parameter internally..but when i get that value i couldnt access the paramter which is present in the table. I can see the tables parameter in the original function in the form of
[[table:0x0989]]
{
[[table:0x23456]]
str = "hello"
width = 180
},
[[table:0x23489]]
{
str1 = "world"
}
it shows like this.but when it returns once i can able to get the top address of table like [[table:0x0989]]..when i tried acessing the tables which is present inside the main table.it is showing a nil value...how do i call that ?? can anyone help me??
If I'm reading it correctly you're doing this:
function my_function ()
--do something
return ({a=1, b=2, c=3})
end
From that you should be able to do this:
my_table = my_function()
then
print(my_table.a) --=> 1
print(my_table.b) --=> 2
print(my_table.c) --=> 3