Lua table.insert - strange behaviour when inserting to table - lua

I'm having a bit of a hard time figuring out why my code is behaving the way it is. I have a table named players, structured as key, value pairs. The values are also nested tables (items). Each item, should contain at least one unique identifier, named 'id', contained withing the nested table (key name) 'specs'.
players = {
['identifier_one'] = {
{
["label"] = "Flashlight",
["weight"] = 1,
["name"] = "WEAPON_FLASHLIGHT",
["specs"] = {
["pockets"] = false,
["id"] = "ZT345", --This is our unique identifier
["equip"] = true,
["stack"] = 1,
["craft"] = true
}
},
{
["label"] = "Flashlight",
["weight"] = 1,
["name"] = "WEAPON_FLASHLIGHT",
["specs"] = {
["pockets"] = false,
["id"] = "ACF124",
["equip"] = true,
["stack"] = 1,
["craft"] = true
}
}
},
['another_item'] = {
...
},
}
I've got the data structure writing correctly. However, when I insert a new nested item, it's adding the newItem to the next increment within the table as expected, and at the same time it's overwriting all of the previus indexes. So the previous items ['spec']['id'] becomes the newItem id if that makes sense? So looking at the above layout, the previous id 'ZT345' would become 'ACF124'. Thus making the two items identical.
When I print out what the newItem consists off, everything is perfect and should write to the table as intended. At first, I thought it may have been something related to the encoding the table into json when writing to the database. However, if I print out the players[identifier] table, after inserting the newItem, it has then overwritten all previous indexes. So I'm pretty certain the issue is upon inserting the newItem to my players[identifier]
My code is as follows:
ESX.RegisterServerCallback('zombie-inventory:itemCheck', function(source, cb, item)
--check our item is allowed to be added.
if items[item] then
local src = source
local identifier = getIdentifier(src)
local newItem = items[item]
if string.match(item, 'WEAPON') then
newItem['specs'][1].id = genId(players[identifier])
end
--This point is where the code falls apart.
table.insert(players[identifier], newItem)
MySQL.Async.execute('UPDATE `user_inventory` SET `inventory` = #inventory WHERE `identifier` = #identifier', {
['#identifier'] = identifier,
['#inventory'] = json.encode(players[identifier])
})
cb(true)
else
print(('[inventory] [^3WARNING^7] invalid item for itemCheck cb: %s'):format(item))
end
end)
Just to add, the genId() function is 100% returning a unique id, I've printed the id numerous times, and it's always different.

Related

How do I get information from a table in lua?

I have some lua code like this:
_table = {
stuff = {
item1 = {Name="Stack",Rarity="Over"};
item2 = {Name="Flow",Rarity="Com"}
};
};
print("placeholder") -- example thing
_stuff = _table.stuff
for i = 1, #_stuff do
print(_stuff[i].Name)
end
The output is this:
placeholder
I've tried to look at stuff but I don't think it was related to my problem.
Lua tables conceptually are "maps": They map keys to values. Your table _table maps the key stuff to the {item1 = ..., item2 = ...} table. Your stuff table maps the string "item1" to the {Name="Stack",Rarity="Over"} table and the string "item2" to the {Name="Flow",Rarity="Com"} table.
The length operator # completely ignores string keys. It returns an integer i such that t[i] ~= nil and t[i+1] == nil. In particular, it returns 0 if there are no integer keys in the table. This is the case for your stuff table: You only have string keys. Thus your loop never runs, as the limit is 0 and i = 1 > 0 already is the case before the first loop iteration.
Put simply, you're trying to iterate over a "dictionary" (a table with string keys) as if it were a list. Lua tables have both a "list part" (integer keys from 1 to n, where usually n = #t) and a "hash part" (of all other keys). The "list part" is empty in your case, so there's nothing for you to iterate over.
To iterate over all key-value pairs of any table (list or dictionary) in an undefined order, use pairs (rather than ipairs or iterating over indices i, which only works for lists):
for itemname, item in pairs(_stuff) do
print(item.Name)
end
If you want a defined order of iteration, either build a second table of keys...
local order = {"item1", "item2"}
for _, key in ipairs(order) do
print(_stuff[key].Name)
end
... or use the integer keys ("list part") of the table:
_table = {
stuff = {
{key = "item1", Name="Stack", Rarity="Over"};
{key = "item2", Name="Flow", Rarity="Com"};
};
};
_stuff = _table.stuff
for _, key in ipairs(order) do
for i = 1, #_stuff do
print(_stuff[i].Name)
end
end
then works as expected.

Best way to saving completed progress in table?

So, i have empty table.
local data_tbl = {}
But i need save data when user complete current task.
Like:
-- (1, 2, 21) it's task id.
data_tbl['1'] = true
data_tbl['2'] = true
data_tbl['21'] = true
And in table "data_tbl" it's look like
data_tbl {
1 = true,
2 = true,
21 = true,
}
I wan't saving only completed task. Not all , because it's very heavy ?
But when i wanted remove this key , like 21 , i'ts get nothing effect.
table.remove(data_tbl, 21)
Task with 21 index not removed. Why ?
Maybe have other best way for all this ? (Saving complete tasks)
I don't want use table.Add() because it's added to last position. Like table.Add(data_tbl, {completed_id = 21})
And in table it's look like.
data_tbl {
1 = {completed_id = 21}
}
-- It's get me more table checks... and cycles , i don't want.
table.insert and table.remove are meant for sequences, but you're using the table as a map. If you have a key, say 21 and want to unset it, just do data_tbl[21] = nil
lua tables are hash tables, to insert data to table use mytable[name] = value or mytable.myname = value, to remove from table mytable[name] = nil or mytable.myname = nil.
To simulate arrays lua tables has helper functions in module table and some sort of optimizations, but id's must be numbers and begin with 1.
For mixed tables like mytable[1] = true; mytable[2] = true; mytable[3] = true; mytable[21] = true; mytable.name = 'values', #mytable evaluates to length of 3 as entries [21] and 'name' do not form the sequence.
table.remove and table.insert modifies 'array' shifting corresponding elements:
local mytable = {1, 2, 3, 4}
table.remove(mytable, 2)
print(table.concat(mytable, ',')) -- result is {1, 3, 4 }
table.insert(mytable, 2, 5)
print(table.concat(mytable, ',')) -- result is {1, 5, 3, 4 }
Numeric strings as keys will be not converted internally into numbers (like in javascript) and will not work with array like methods:
local mytable = {}
mytable[21] = 'value21'
mytable['21'] = 'string21'
-- table content: {[21] = 'value21', ['21'] = 'string21'}

How can i keep this table in the shown order?

Hello i have got a table, that uses string indexes:
shirt = {
["shirtwhite.png"] = "shirt_white.png",
["shirtwhite.png^[multiply:#3f3f3f"] = "shirt_white.png^[multiply:#3f3f3f",
["shirtwhite.png^[multiply:#ff0000"] = "shirt_white.png^[multiply:#ff0000",
["shirtwhite.png^[multiply:#ff7f00"] = "shirt_white.png^[multiply:#ff7f00",
["shirtwhite.png^[multiply:#ffff00"] = "shirt_white.png^[multiply:#ffff00",
["shirtwhite.png^[multiply:#00ff00"] = "shirt_white.png^[multiply:#00ff00",
["shirtwhite.png^[multiply:#0000ff"] = "shirt_white.png^[multiply:#0000ff",
["shirtwhite.png^[multiply:#9f00ff"] = "shirt_white.png^[multiply:#9f00ff",
},
Theese are t-shirt-textures for an editable game-character-skin (with colour-values for different colors).
There are some more of theese tables in the code, for other parts of the character-skin
how can I keep the table in it´s shown order, while it´s loaded in this code-snippet?
The tzables are in a file "skins.lua" and the code-snippet is from another lua-file
character_creator = {}
character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")
local skins = character_creator.skins
local skins_array = {}
minetest.after(0, function()
local function associative_to_array(associative)
local array = {}
for key in pairs(associative) do
table.insert(array, key)
end
return array
end
skins_array = {
skin = associative_to_array(skins.skin),
hair = associative_to_array(skins.hair),
eyes = associative_to_array(skins.eyes),
shirt = associative_to_array(skins.shirt),
pants = associative_to_array(skins.pants),
}
end)
In Lua only arrays (positive integer-indexed tables) have "order" (can be iterated using ipairs); the hash tables (like the one you are working with) are unordered. If you want to iterate over a table like this in a specific order, you'd usually create an array with the keys, sorted them in the order you want and then iterate over that array extracting elements from your table.
There are also components (like ordered table) that may keep track of insertions and return results in the same order, if that's what you want.

lua: user input to reference table

I am having trouble with my tables, I am making a text adventure in lua
local locxy = {}
locxy[1] = {}
locxy[1][1] = {}
locxy[1][1]["locdesc"] = "dungeon cell"
locxy[1][1]["items"] = {"nothing"}
locxy[1][1]["monsters"] = {monster1}
The [1][1] refers to x,y coordinates and using a move command I can successfully move into different rooms and receive the description of said room.
Items and monsters are nested tables since multiple items can be held there (each with their own properties).
The problem I am having is getting the items/monsters part to work. I have a separate table such as:
local monsters = {}
monsters["rat"] = {}
monsters["rat"]["Name"] = "a rat"
monsters["rat"]["Health"] = 5
monsters["rat"]["Attack"] = 1
I am using a table like this to create outlines for various enemy types. The monster1 is a variable I can insert into the location table to call upon one of these outlines, however I don't know how to reference it.
print("You are in ", locxy[x][y]["locdesc"]) -- this works
print("You can see a ", locxy[x][y]["monsters]["Name"],".") - does not work
So I would like to know how I can get that to work, I may need a different approach which is fine since I am learning. But I would also specifically like to know how to / if it possible to use a variable within a table entry that points to data in a separate table.
Thanks for any help that can be offered!
This line
locxy[x][y]["monsters]["Name"]
says
look in the locxy table for the x field
then look in the y field of that value
look in the "monsters"` field of that value
then look in the "Name" field of that value
The problem is that the table you get back from locxy[x][y]["monsters"] doesn't have a "Name" field. It has some number of entries in numerical indices.
locxy[x][y]["monsters][1]["Name"] will get you the name of the first monster in that table but you will need to loop over the monsters table to get all of them.
Style notes:
Instead of:
tab = {}
tab[1] = {}
tab[1][1] = {}
you can just use:
tab = {
[1] = {
{}
}
}
and instead of:
monsters = {}
monsters["rat"] = {}
monsters["rat"]["Name"] = "foo"
you can just use:
monsters = {
rat = {
Name = "foo"
}
}
Or ["rat"] and ["Name"] if you want to be explicit in your keys.
Similarly instead of monsters["rat"]["Name"] you can use monsters.rat.Name.

Can I make table from other table elements?

Am I able to make a separated table filled with elements from other tables in Lua? Like this:
TableA = { a=1, b=2, c=3 }
TableB = { John=4, Jane =5 }
TableC = { x = "asd", y = "dsa", z = "sda" }
TableAll = { TableA.a, TableB.John, TableC.x}
It's a general example, but may work. I'm not sure in it.
Is there some reason why that wouldn't work?
All that tablename.key does is access an element of a table. This is a value; the fact that it came from a table is completely irrelevant. The value returned by tablename.key is no different than the value 5, nil, true, "some string", or any other value in Lua.
You can initialize a table with values; it doesn't matter where those values come from.

Resources