Pass Lua table by stringified reference instead of directly by reference - lua

I want to pass a lua reference to another function without actually using assignment = but something like loadstring.
local myTable = { test="Hello" }
local myTableStringified = tostring(myTable) -- table: 0xref
print(myTableStringified)
local myTableUnstringified = loadstring(myTableStringified)
print(myTableUnstringified) -- nil but should show table: 0xref
As seen above, this won't do.

You'll have to use one of the modules that provide serialization.
Keep in mind that loadstring returns a function that needs to be called, so to get the table back you need to use loadstring(myTableStringified)().

Related

Lua puts of variable name instead of variable value in table

Hello guys i am trying to add Divisions to a game but there is a problem with tables, do you guys know how to solve this?
local divisonName = result3[i].name
print(divisonName)
ESX.Divisions[result3[i].owner].divisonName = {}
it's the code it should get division name and create a table with that like this (we assume divisonName gonna return swat for example):
["police"] = {
["swat"] = {
},
},
but instead of putting division name as SWAT it will put divisionName variable
i already print that divisionName variable and console return me SWAT so everything is fine with logic and value of variable but i guess there it's syntax issue i am not sure!
Console Debug image
Note that in Lua the construct some_table.field is a syntactic sugar for some_table["field"]. Whatever is written after the dot will be treated as a string key.
In your case, to index by the value stored in the variable, you need to write ESX.Divisions[result3[i].owner][divisonName], and not as .divisionName.

Metamethod when accessing a key as mutable

__index is called when accessing as immutable :
local foo = bar["foo"];
__newindex is called when access as mutable an index that doesn't exist :
local bar = { }
bar["foo"] = 123 -- calls __newindex
bar["foo"] = 456 -- does NOT call __newindex
Is there a metamethod that can be called when accessing a key as mutable evey time, i.e not only if the key doesn't exist yet?
I would like to create a behavior so that when a users sets a key in a table, it calls a native method instead, regardless if the key already exists or not.
The standard way to do what you want is to use a proxy table, that is an empty table with suitable metamethods to access the actual table. Since the proxy is empty, the metamethods are called every time you get or set fields in it.
I am rather sure there are no such metamethods you ask for.
But you can try make a workaround to get what you want.
For example you can try to use the __call metamethod in this way:
local mt = {}
function mt.__call(tbl, key, val)
-- this is called every time you use bar(key, val)
tbl[key] = val
end
local bar = setmetatable({}, mt)
bar("foo", 123)
bar("foo", 456)
print(bar.foo)
Or you could use a function in some other way to achieve this.
Immutability doesn't exist in Lua, you just mean indexed accessing and assigning. Lua 5.3 states...
This event happens when table is not a table or when key is not
present in table.
...for both cases.
Your best option would be to store values in another table or subtable of yours.

Lua dont allow table append

I already have a lua-table A={}, A.B={} under global variable. I have a function on the call of what creates lua table D1={}, D1.D2={}, but the porblem is this function places the table in global variable list. When i print all lua values, it prints:
A={}, A.B={}, D1=={}, D1.D2={}. Is there a way I can create the function under table under A={}, A.B={} which mean i want output as:
A={}, A.B={}, A.B.D1=={}, A.B.D1.D2={}. I dont want to use table.insert() since the hirarchy of source-table is not known.
It sounds like what you want to do here, is pass the table to the function that creates D1 and D1.D2 so you can append those values wherever you want them.
function addTable(tbl)
tbl.D1 = {}
tbl.D1.D2 = {'test'}
end
addTable(A.B)
-- now you can call A.B.D1.D2
print(A.B.D1.D2[1]) -- prints 'test'

Creating a table with a string name

I've created a lot of string variable names and I would like to use the names as table names ie:
sName1 = "test"
sName2 = "test2"
tsName1 ={} -- would like this to be ttest ={}
tsName2 ={} -- ttest2 = {}
I can't figure out how to get this to work, have gone through various combinations of [] and .'s but on running I always get an indexing error, any help would be appreciated.
In addition to using _G, as Mike suggested, you can simply put all of these tables in another table:
tables = { }
tables[sName1] = { }
While _G works the same way pretty much every table does, polluting global "namespace" isn't much useful except for rare cases, and you'll be much better off with a regular table.
your question is sort of vague, but i'm assuming you want to make tables that are named based off of string variables. one way would be to dynamically create them as global objects like this:
local sName1 = "test"
-- this creates a name for the new table, and creates it as a global object
local tblName = "t".. sName1
_G[tblName] = {}
-- get a reference to the table and put something in it.
local ttest = _G[tblName]
table.insert(ttest, "asdf")
-- this just shows that you can modify the global object using just the reference
print(_G[tblName][1])

Can Lua support case-insensitive method calls?

I'm using Lua as a data description language for my C++ app. I have a bunch of C++ classes bound to Lua using SLB 2.0. I have methods bound such as 'SetPos' or 'SetName'. I specify the position or name (for example) using a table with values keyed as 'pos' or 'name'. I want to be able to take the key, prepend 'set', and call the method, if it exists (it may not). Is that possible? If so, any suggestions?
I know I could make my bound methods lower case, but I'd rather keep them the same as the methods they're bound to (that may be my fallback though). I could try to build the method name based on my naming standards, but case insensitivity is less error prone.
I feel there should be a tricky piece of Lua that could solve this using metatables, but I haven't been able to work it out myself.
Any suggestions?
Thanks!
Case insensitivity is not really something Lua handles. All table lookups and local variable accesses are ultimately case sensitive string compares.
The best solution would be to just accept that you're dealing with a case sensitive system, just like C++, and deal with it.
However, if you really want to, you can do this. The simplest way would be to put every possible case permutation of a name in your function table. So your function table would have this:
["setname"] = theFunction,
["Setname"] = theFunction,
["sEtname"] = theFunction,
["SEtname"] = theFunction,
...
You can of course automate this with a function that takes each name in the table and replicates its data based on the case permutations.
A more involved but easier to use mechanism would be to use the __index and __newindex metamethods along with the empty table trick.
function CreateCaseInsensitiveTable()
local metatbl = {}
function metatbl.__index(table, key)
if(type(key) == "string") then
key = key:lower()
end
return rawget(table, key)
end
function metatbl.__newindex(table, key, value)
if(type(key) == "string") then
key = key:lower()
end
rawset(table, key, value)
end
local ret = {}
setmetatable(ret, metatbl)
return ret
end
Instead of creating a table with {}, you create the table with this function call. The table should otherwise function as normal (though obviously member access will be slightly slower).

Resources