This is how i’m checking and receiving items right now (and it is not working well):
local function retrieveItemFromData(item)
for _,v in pairs(itemsData) do
print(_,v) -- outputs below
for i,x in pairs(v) do
if x.Name == item then
return x
else
warn(item.." does not exist in the game data, please check for typos.")
end
--break
end
end
end
Calling 2 or more items starts to cause weird behaviors and outputs:
local item = retrieveItemFromData("Test Item")
local item2 = retrieveItemFromData("Vigilant Ash")
-- output
Items table: 0xea88d181b69bbf00 (x2)
15:38:37.770 - Vigilant Ash does not exist in the game data, please check for typos.
Consumables table: 0x395d92130f2c1680
Test Item 784F0284-50A2-4DDB-8C8C-C927AA12F56D
New tag editor version coming online; unloading the old version (x2)
First line prints the Items table twice, for some reason while the Consumables table is called once and it also says Vigilant Ash does not exist which is and else statement on the function that retrieves the items. Now I definitely did something wrong with this and I need help.
Every time i’m working with tables like this i’m getting confused and messed up
Tables arent bad at all, its more the approach you’ve taken to using them. It may be more effient to do it this way, but it will just make things more complicated. Keep it clear and simple when it comes to tables. Tables are all organised values, so if you are organised using them should actually make life a lot easier.
Seems like my table is quite organized, with 2 different categories and 1 item in each category, and what I am trying to approach is checking if an item exists within all the categories.
The output is right. You warn if it’s not in the first table you check, rather than if it’s not in any table. Remember that the code in a for loop runs individually for every key-value pair it loops through.
Edit: Basically:
The first time around, it goes into Items, finds Test Item, and returns. Only Items is printed since it didn’t need to check consumables.
The second time around, it goes into Items again, doesn’t find the Ash, and warns. It then checks the Consumables and finds the Ash, and returns.
So where can I fit the warning output? Sorry I am really trying hard to understand xD
I have two loops, putting it after the second loop still warns even if both items exist.
local function getDataFromItemName(item)
for _,v in pairs(itemsData) do
for catName, cat in pairs(v) do
for _, i in pairs(cat) do
if i.Name == item then
return i
end
end
end
end
warn(item.." does not exist in the game data, please check for typos.")
end
You do not need more than two loops if you explicitly index with Items or Consumables in your code, or if you explicitly index the first table you get from the module. However, using a third loop ensures that no matter how many tables coexist, the function will always catch everything. So if for example, you added a third table to your module, or a third category, having three loops makes it not a problem, as opposed to having only two, where you have to edit the code again.