Originally Posted by Marsgames
Hey,
I have a new question. Is it possible to convert an itemString into an itemLink without using GetItemInfo() to avoid the possible nil due to item not in cache
|
They're pretty much interchangeable.
Just store item ID of an item that isn't cached in some table, then compare it to an item ID that comes w/ "GET_ITEM_INFO_RECEIVED", then re-request item's info and save it.
And, IMHO, it's better to use item strings for storing item's info in SVs.
Lua Code:
local frame = CreateFrame("Frame")
local notCached = {}
frame:SetScript("OnEvent", function(_, event, arg1)
if event == "PLAYER_LOGIN" then
local slotID = GetInventorySlotInfo("Trinket0Slot")
local itemLink = GetInventoryItemLink("player", slotID)
local itemID = GetInventoryItemID("player", slotID)
local itemString, itemName
if itemLink then
itemString, itemName = itemLink:match("|H(.*)|h%[(.*)%]|h")
end
-- if there's no itemID, then there's no item :p
if itemID then
if not itemName or itemName == "" then
GetItemInfo(itemID) -- for GET_ITEM_INFO_RECEIVED
notCached[itemID] = slotID
else
-- do stuff here
end
end
elseif event == "GET_ITEM_INFO_RECEIVED" then
if notCached[arg1] then
-- do stuff here
-- remove item's ID from the table
notCached[arg1] = nil
end
end
end)
frame:RegisterEvent("GET_ITEM_INFO_RECEIVED")
frame:RegisterEvent("PLAYER_LOGIN")
-- edit #1
Something like this
Lua Code:
local frame = CreateFrame("Frame")
local notCached = {}
local equippedItems = {
[INVSLOT_HEAD] = false, [INVSLOT_HAND] = false,
[INVSLOT_NECK] = false, [INVSLOT_WAIST] = false,
[INVSLOT_SHOULDER] = false, [INVSLOT_LEGS] = false,
[INVSLOT_BACK] = false, [INVSLOT_FEET] = false,
[INVSLOT_CHEST] = false, [INVSLOT_FINGER1] = false,
[INVSLOT_BODY] = false, [INVSLOT_FINGER2] = false,
[INVSLOT_TABARD] = false, [INVSLOT_TRINKET1] = false,
[INVSLOT_WRIST] = false, [INVSLOT_TRINKET2] = false,
[INVSLOT_MAINHAND] = false, [INVSLOT_OFFHAND] = false,
}
local function getEquippedItemString(slotID)
local itemLink = GetInventoryItemLink("player", slotID)
local itemID = GetInventoryItemID("player", slotID)
local itemString, itemName
if itemLink then
itemString, itemName = itemLink:match("|H(.*)|h%[(.*)%]|h")
end
-- if there's no itemID, then there's no item :p
if itemID then
if not itemName or itemName == "" then
GetItemInfo(itemID) -- for GET_ITEM_INFO_RECEIVED
notCached[itemID] = slotID
else
return itemString
end
else
return false
end
end
frame:SetScript("OnEvent", function(_, event, arg1)
if event == "PLAYER_LOGIN" then
for slotID in next, equippedItems do
equippedItems[slotID] = getEquippedItemString(slotID)
end
elseif event == "GET_ITEM_INFO_RECEIVED" then
if notCached[arg1] then
local slotID = notCached[arg1]
notCached[arg1] = nil
equippedItems[slotID] = getEquippedItemString(slotID)
end
end
end)
frame:RegisterEvent("GET_ITEM_INFO_RECEIVED")
frame:RegisterEvent("PLAYER_LOGIN")
equippedItems is a table where indices are slot IDs, I organised them in a way that resembles their placement on char info panel.