Lua 在 table 中添加和过滤项目
Lua adding and filtering items in a table
我正在编写一个 WoW 插件,它将跟踪和过滤项目列表。
已添加不在列表中的项目;不应添加已列出的项目。
我遇到的问题是我的检查功能无法始终如一地防止将重复项添加到列表中。
第一次添加项目 A 效果很好,如果它是最后添加的东西,则尝试再次重新添加它是正确的,而不是重新添加。
第一次添加项目 B 工作正常,如果它是最后添加的东西,则尝试重新添加它是正确的,而不是重新添加。
问题是如果我尝试重新添加项目 A 而不是最后添加的项目,它会错误地将项目重新添加到列表中;所以基本上我可以重新添加项目,只要它们不是最后添加的项目。
Here is a gif that shows you what is happening;
这是我的两个函数。
我也链接了我的 full lua code here and the toc here。
-- check if item is already listed
local function isItemOnList(incomingItemID)
for k, v in pairs(AAAGlobalItemLinkList) do
if v.itemID == incomingItemID then
print(v.itemID, ':matched:', incomingItemID)
return true
end
print('no match', incomingItemID)
return false
end
end
-- add item link to list function
local function addItemLinkToList()
local cursorItemType, cursorItemID, cursorItemLink = GetCursorInfo()
print(cursorItemID)
if not isItemOnList(cursorItemID) then
local itemName,
itemLink,
itemRarity,
itemLevel,
itemMinLevel,
itemType,
itemSubType,
itemStackCount,
itemEquipLoc,
iconFileDataID,
itemSellPrice,
itemClassID,
itemSubClassID,
bindType,
expacID,
itemSetID,
isCraftingReagent = GetItemInfo(cursorItemID)
tempItemList = {
itemID = cursorItemID,
itemName = itemName,
itemLink = itemLink,
itemRarity = itemRarity,
itemLevel = itemLevel,
itemMinLevel = itemMinLevel,
itemType = itemType,
itemSubType = itemSubType,
itemStackCount = itemStackCount,
itemEquipLoc = itemEquipLoc,
iconFileDataID = iconFileDataID,
itemSellPrice = itemSellPrice,
itemClassID = itemClassID,
itemSubClassID = itemSubClassID,
bindType = bindType,
expacID = expacID,
itemSetID = itemSetID,
isCraftingReagent = isCraftingReagent
}
table.insert(AAAGlobalItemLinkList, 1, tempItemList)
print(cursorItemLink, 'added to list')
else
print(cursorItemLink, 'already listed')
end
updateScrollFrame()
ClearCursor()
end
-- update scroll frames function
local function updateScrollFrame()
wipe(listItems)
for index = 1, #AAAGlobalItemLinkList do
--if testItemRarity(AAAGlobalItemLinkList[index]) then
table.insert(listItems, AAAGlobalItemLinkList[index]['itemLink'])
--end
end
FauxScrollFrame_Update(testingScrollFrame, #listItems, NumberOfButtons, HeightOfButtons)
for index = 1, NumberOfButtons do
local offset = index + FauxScrollFrame_GetOffset(testingScrollFrame)
local button = testingScrollFrame.buttons[index]
if index > #listItems then
button:SetText('')a
button.index = nil
else
button.index = offset
--local itemName, itemLink = GetItemInfo(listItems[offset])
button:SetText(listItems[offset])
end
end
end
我没有收到任何错误。
我还在此处链接了完整的 lua 代码和目录。
希望有人可以解释我是如何搞砸的,也可以指出正确的方向来解决它。
在您的函数 isItemOnList
中,您 return 在 for
循环中为 false,因此循环不能执行超过 1 次迭代。您应该将 return false
放在 for
循环之外:
-- check if item is already listed
local function isItemOnList(incomingItemID)
for k, v in pairs(AAAGlobalItemLinkList) do
if v.itemID == incomingItemID then
print(v.itemID, ':matched:', incomingItemID)
return true
end
end
print('no match', incomingItemID)
return false
end
你也可以不使用 return,所以 nil
默认会被 return 编辑,对于 if
检查 nil
与false
我正在编写一个 WoW 插件,它将跟踪和过滤项目列表。
已添加不在列表中的项目;不应添加已列出的项目。
我遇到的问题是我的检查功能无法始终如一地防止将重复项添加到列表中。
第一次添加项目 A 效果很好,如果它是最后添加的东西,则尝试再次重新添加它是正确的,而不是重新添加。
第一次添加项目 B 工作正常,如果它是最后添加的东西,则尝试重新添加它是正确的,而不是重新添加。
问题是如果我尝试重新添加项目 A 而不是最后添加的项目,它会错误地将项目重新添加到列表中;所以基本上我可以重新添加项目,只要它们不是最后添加的项目。
Here is a gif that shows you what is happening;
这是我的两个函数。
我也链接了我的 full lua code here and the toc here。
-- check if item is already listed
local function isItemOnList(incomingItemID)
for k, v in pairs(AAAGlobalItemLinkList) do
if v.itemID == incomingItemID then
print(v.itemID, ':matched:', incomingItemID)
return true
end
print('no match', incomingItemID)
return false
end
end
-- add item link to list function
local function addItemLinkToList()
local cursorItemType, cursorItemID, cursorItemLink = GetCursorInfo()
print(cursorItemID)
if not isItemOnList(cursorItemID) then
local itemName,
itemLink,
itemRarity,
itemLevel,
itemMinLevel,
itemType,
itemSubType,
itemStackCount,
itemEquipLoc,
iconFileDataID,
itemSellPrice,
itemClassID,
itemSubClassID,
bindType,
expacID,
itemSetID,
isCraftingReagent = GetItemInfo(cursorItemID)
tempItemList = {
itemID = cursorItemID,
itemName = itemName,
itemLink = itemLink,
itemRarity = itemRarity,
itemLevel = itemLevel,
itemMinLevel = itemMinLevel,
itemType = itemType,
itemSubType = itemSubType,
itemStackCount = itemStackCount,
itemEquipLoc = itemEquipLoc,
iconFileDataID = iconFileDataID,
itemSellPrice = itemSellPrice,
itemClassID = itemClassID,
itemSubClassID = itemSubClassID,
bindType = bindType,
expacID = expacID,
itemSetID = itemSetID,
isCraftingReagent = isCraftingReagent
}
table.insert(AAAGlobalItemLinkList, 1, tempItemList)
print(cursorItemLink, 'added to list')
else
print(cursorItemLink, 'already listed')
end
updateScrollFrame()
ClearCursor()
end
-- update scroll frames function
local function updateScrollFrame()
wipe(listItems)
for index = 1, #AAAGlobalItemLinkList do
--if testItemRarity(AAAGlobalItemLinkList[index]) then
table.insert(listItems, AAAGlobalItemLinkList[index]['itemLink'])
--end
end
FauxScrollFrame_Update(testingScrollFrame, #listItems, NumberOfButtons, HeightOfButtons)
for index = 1, NumberOfButtons do
local offset = index + FauxScrollFrame_GetOffset(testingScrollFrame)
local button = testingScrollFrame.buttons[index]
if index > #listItems then
button:SetText('')a
button.index = nil
else
button.index = offset
--local itemName, itemLink = GetItemInfo(listItems[offset])
button:SetText(listItems[offset])
end
end
end
我没有收到任何错误。
我还在此处链接了完整的 lua 代码和目录。
希望有人可以解释我是如何搞砸的,也可以指出正确的方向来解决它。
在您的函数 isItemOnList
中,您 return 在 for
循环中为 false,因此循环不能执行超过 1 次迭代。您应该将 return false
放在 for
循环之外:
-- check if item is already listed
local function isItemOnList(incomingItemID)
for k, v in pairs(AAAGlobalItemLinkList) do
if v.itemID == incomingItemID then
print(v.itemID, ':matched:', incomingItemID)
return true
end
end
print('no match', incomingItemID)
return false
end
你也可以不使用 return,所以 nil
默认会被 return 编辑,对于 if
检查 nil
与false