View Single Post
06-25-21, 02:39 AM   #1
henbe
A Defias Bandit
Join Date: Jun 2021
Posts: 3
Show/Hide Custom Frame in Combat Lockdown

Hello everyone, Henbe here. I am quite inexperienced in lua and coding in general, please help me understand how to show/hide frames in secured enviroment. I have a little piece of code that creates a frame with texture and shows/hides on toggling bags. Outside of combat it works just fine, but I get an error while in combat.

Lua Code:
  1. -- Button
  2. local BB = CreateFrame("Button","FBags_BagButton",UIParent,"SecureActionButtonTemplate")
  3. BB:SetPoint("CENTER", UIParent, "CENTER",0,500)
  4. BB:SetSize(50, 50)
  5. local ButtonTexture = BB:CreateTexture("ButtonTexture", "BACKGROUND")
  6. ButtonTexture:SetTexture("Interface\\Buttons\\Button-Backpack-Up")
  7. ButtonTexture:SetPoint("CENTER","FBags_BagButton","CENTER")
  8. ButtonTexture:SetSize(50, 50)
  9. BB:SetScript("OnClick", function(self)
  10.     FB:SetShown(not FB:IsShown())
  11.     print("Bag Clicked")
  12. end)
  13. BB:RegisterEvent("PLAYER_ENTERING_WORLD")
  14.  
  15.  
  16.  
  17. FB = CreateFrame("Button","FBags",UIParent, "ActionButtonTemplate")
  18. FB:SetFrameStrata("BACKGROUND")
  19. FB:SetPoint("CENTER", UIParent, "CENTER",-400,300)
  20. FB:SetSize(50, 50)
  21. FB:Hide()
  22.  
  23.     -- Bag Layout
  24. for bagid = 0,4 do
  25.     for bagslot = 1,GetContainerNumSlots(bagid) do
  26.         local icon, itemCount, locked,  ContItemQuality, readable, lootable, ContItemLink, isFiltered, noValue, ContItemID = GetContainerItemInfo(bagid, bagslot)
  27.         local SlotFrame = CreateFrame("Button", "Bag"..bagid.."Slot"..bagslot, FB,"SecureActionButtonTemplate, ActionButtonTemplate",100*bagid+bagslot)
  28.        
  29.         SlotFrame:SetSize(40,40)
  30.         SlotFrame:SetPoint("TOP",FB,"BOTTOM",40*bagslot,-40*bagid)
  31.        
  32.         SlotFrame:RegisterEvent("BAG_UPDATE")
  33.         SlotFrame:RegisterEvent("ITEM_LOCKED")
  34.         SlotFrame:RegisterEvent("ITEM_UNLOCKED")
  35.         SlotFrame:RegisterForClicks("AnyUp")
  36.        
  37.             -- to use items
  38.         SlotFrame:SetAttribute("type2", "item")
  39.         SlotFrame:SetAttribute("bag", bagid)
  40.         SlotFrame:SetAttribute("slot", bagslot)
  41.  
  42.  
  43.             -- adding a texture, text, cooldown
  44.         local texture = SlotFrame:CreateTexture("Bag"..bagid.."Slot"..bagslot.."Texture", "BACKGROUND")
  45.         local text = SlotFrame:CreateFontString("Bag"..bagid.."Slot"..bagslot.."Text", "OVERLAY")
  46.         text:SetFont("Fonts\\FRIZQT___CYR.TTF", 12, "OUTLINE")
  47.         text:SetPoint("BOTTOMRIGHT",SlotFrame,"BOTTOMRIGHT")
  48.         local cooldown = CreateFrame("Cooldown", "Bag"..bagid.."Slot"..bagslot.."Cooldown", SlotFrame, "CooldownFrameTemplate")
  49.         cooldown:SetAllPoints()
  50.  
  51.             -- widget scripts
  52.                 -- Tooltip
  53.         SlotFrame:SetScript("OnEnter", function()
  54.             GameTooltip:SetOwner(SlotFrame, "ANCHOR_CURSOR")
  55.             GameTooltip:SetBagItem(bagid, bagslot); -- SetBagItem(bagid, bagslot)
  56.             GameTooltip:Show()
  57.         end)
  58.        
  59.         SlotFrame:SetScript("OnLeave", function()
  60.             GameTooltip:Hide()
  61.         end)
  62.  
  63.                 -- Item moving
  64.         SlotFrame:SetScript("OnMouseUp", function(self, button)
  65.             local CursorItem, CursorItemID, CursorItemLink = GetCursorInfo()
  66.             local x = GetMouseFocus():GetID()
  67.             local b = (x-x%100)/100
  68.             local s = x%100
  69.             local icon1, itemCount1, locked1, quality1, readable1, lootable1, itemLink1, isFiltered1, noValue1, ContItemID1 = GetContainerItemInfo(b, s)
  70.             if not (b == 0 and s == 0) then
  71.                 if button == "LeftButton" then
  72.                     if GetContainerItemInfo(b, s) and not GetCursorInfo() then
  73.                         PickupContainerItem(b, s)
  74.                         print("Picking up bag "..b.." slot "..s.." using "..button)
  75.                     elseif GetCursorInfo() and not GetContainerItemInfo(b, s) then
  76.                         PickupContainerItem(b, s)
  77.                         print("Putting down bag "..b.." slot "..s.." using "..button)
  78.                     elseif not GetCursorInfo() and not GetContainerItemInfo(b, s) then
  79.                         print("Nothing to pick up")
  80.                     elseif ContItemID1 == 82800 then
  81.                         PickupContainerItem(b, s)
  82.                         print("Putting down bag "..b.." slot "..s.." using "..button)
  83.                     elseif CursorItemID == ContItemID1 then
  84.                         ClearCursor()
  85.                         print("Same cell, putting down")
  86.                     elseif CursorItemID ~= ContItemID1 then
  87.                         PickupContainerItem(b, s)
  88.                         print("Filled cell, swapping")
  89.                     end
  90.                 end
  91.             end
  92.         end)
  93.  
  94.  
  95.             -- Bag Update, cooldowns, icons
  96.         local function eventHandler(self, event, ...)
  97.             local icon, itemCount, locked, quality, readable, lootable, itemLink, isFiltered, noValue, itemID = GetContainerItemInfo(bagid, bagslot)
  98.             local startTime, duration, isEnabled = GetContainerItemCooldown(bagid, bagslot)
  99.             if event == "BAG_UPDATE" then
  100.                 if GetContainerItemInfo(bagid, bagslot) then
  101.                     if duration>0 then
  102.                         cooldown:SetCooldown(startTime,duration)
  103.                         print("cooldown started in "..bagid.." "..bagslot)
  104.                     else
  105.                         cooldown:SetCooldown(0,0)
  106.                     end
  107.                     texture:SetTexture(icon)
  108.                     texture:SetPoint("CENTER","Bag"..bagid.."Slot"..bagslot,"CENTER")
  109.                     texture:SetSize(40, 40)
  110.                     if itemCount>1 then
  111.                         text:SetText(itemCount)
  112.                     else
  113.                         text:SetText(nil)
  114.                     end
  115.                 else
  116.                     texture:SetTexture("Interface\\BUTTONS\\UI-Slot-Background.blp")
  117.                     texture:SetPoint("CENTER","Bag"..bagid.."Slot"..bagslot,"CENTER",11,-10)
  118.                     texture:SetSize(60, 60)
  119.                     text:SetText()
  120.                     cooldown:SetCooldown(0,0)
  121.                 end
  122.             end
  123.         end
  124.         SlotFrame:SetScript("OnEvent", eventHandler);
  125.     end
  126. end
  127.  
  128.  
  129. -- "OpenBackpack" "CloseBackpack" "ToggleBackpack" "CloseAllBags" "OpenAllBags" "ToggleAllBags"
  130. -- hook functions
  131.  
  132. hooksecurefunc(
  133.     "CloseAllBags",function()
  134.         if FB:IsShown() then
  135.             FB:Hide()
  136.         end
  137.         print("CloseAllBags hookedfunction fired")
  138.     end
  139. )
  140.  
  141. hooksecurefunc(
  142.     "ToggleAllBags",function()
  143.         FB:SetShown(not FB:IsShown())
  144.         print("ToggleAllBags hookedfunction fired")
  145.     end
  146. )

Last edited by henbe : 06-28-21 at 04:14 AM.
  Reply With Quote