Initialising and using a global table - lua

I'm very new to Lua and I'm trying to globally initialise a table at the very start of my program. At the top, I have:
storage = {}
Then, I want to iterate over elements in this table inside functions in the same file. One example is:
local output
for item in storage do
output = output .. item
end
return output
In this case, I get:
attempt to call a nil value
On the line beginning with for.
I have also tried printing out storage[1]. In this case I get:
attempt to index local 'storage' (a nil value)
Could someone please explain in simple terms what could be wrong here?

You are not showing the entire script, but it's clear that storage value gets reset somewhere between your initialization and using in for item in storage do, because if it keeps the value, you'd get a different error: attempt to call a table value.
You need to use ipairs or pairs function in the loop -- for key, item in pairs(storage) do -- but you first need to fix whatever resets the value of storage.

Related

Pass Lua table by stringified reference instead of directly by reference

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)().

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.

is my understanding of the following code correct?

suppose I have a file with name myFileName.lua, which contains the following code.
function Set(source)
set = {}
if source then
for i,v in ipairs(source) do
set[v] = true
end
end
return set
end
return Set
My understanding is as the following: source is a table structure. if source then means, if the table source is not empty, then do something. The first return set means returning the table set as the return value of the function Set. The second return Set means, returning Set function as the return value of this file myFileName.lua.
Then, in file main.lua, I have
Set = require('lib/myFileName')
This means, the Set function in the file myFile.lua is returned and assigned the name Set, so that I can use it in the file main.lua. Is this correct? Any comments are greatly appreciated.
Inside the file, Set is a function. set is a table. The if source then line will just make sure source is not nil or false. so even if source is {} empty, it will enter.
All of the rest is correct, requiring the file from main would give you access to the Set function.
For more information you can read further about logical expressions in Lua here

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'

Resources