WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Help with SetScript (https://www.wowinterface.com/forums/showthread.php?t=54210)

cokedrivers 08-14-16 01:59 PM

Help with SetScript
 
I'm trying to make a clickable button for my data panel that allows the user to one click either a Repair Bot or Repair Mount.

I can not seem to get it to go threw all the options.

Here is the code I'm trying to wwork with:
Code:

        plugin:SetScript("OnMouseDown", function(self, btn)
                local RepairProfession = GetProfessions()
                if btn == "LeftButton" then
                        if RepairProfession == "Engineering" then
                                CastSpellByName("Field Repair Bot 74A") or CastSpellByName("Field Repair Bot 110G") or CastSpellByName("MOLL-E") or CastSpellByName("Scrapbot") or CastSpellByName("Jeeves")
                        else
                                CastSpellByName("Guild Page") or CastSpellByName("Guild Page") or CastSpellByName("Guild Herald") or CastSpellByName("ĽArgent Squire") or CastSpellByName("Argent Gruntling")
                        else
                                print('|cff33ff99cData:|r |cffFF0000No Repair bots Available|r')
                        end
                elseif btn == "MiddleButton" then
                        ToggleCharacter("PaperDollFrame")
                elseif btn == "RightButton" then
                        CastSpellByName("Traveler's Tundra Mammoth") or CastSpellByName("Grand Expedition Yak")
                end               
        end)

I was able to get the CastSpellByName("Traveler's Tundra Mount") to cast the mount spell so I know it works but I keep getting an error with the "or" in the formula.

Thanks for any help with this.
Coke

Vlad 08-15-16 01:51 AM

CastSpell related API are protected, meaning you shouldn't be able to use them, only Blizzards code may use these functions. There are some few exceptions to this, but in general it would be better to go about this issue in a different way, namely using the secure template system.

To get you started:
http://wow.gamepedia.com/SecureActionButtonTemplate

Phanx 08-15-16 02:14 AM

Apart from that, there is an actual syntax error in your code. This isn't valid Lua:

lua Code:
  1. if RepairProfession == "Engineering" then
  2.                 CastSpellByName("Field Repair Bot 74A") or CastSpellByName("Field Repair Bot 110G") or CastSpellByName("MOLL-E") or CastSpellByName("Scrapbot") or CastSpellByName("Jeeves")
  3.             else

That second line would only be valid if it started with a variable assignment ("local x =") or a keyword like "if" or "return".

I assume your intention was "try each of these and cast the first one that's available". Not only is (as Vlad already said) that not allowed -- you can't call CastSpellByName in an addon for any actual spell (only for professions, but there are better ways to open the tradeskill UI that you should use instead of that) -- but even if it was allowed, you couldn't do it that way. You'd need to do something like this instead:

lua Code:
  1. if RepairProfession == "Engineering" then
  2.         if GetItemCount("Field Repair Bot 74A") > 0 and GetItemCooldown(123) == 0 then
  3.             CastSpellByName("Field Repair Bot 74A")
  4.         elseif GetItemCount("Field Repair Bot 110G") > 0 and GetItemCooldown(456) == 0 then
  5.             CastSpellByName("Field Repair Bot 110G")
  6.         elseif GetItemCount("MOLL-E") > 0 and GetItemCooldown(789) == 0 then
  7.             CastSpellByName("MOLL-E")
  8.         elseif GetItemCount("Scrapbot") > 0 and GetItemCooldown(852) == 0 then
  9.             CastSpellByName("Scrapbot")
  10.         elseif GetItemCount("Jeeves") > 0 and GetItemCooldown(159) == 0 then
  11.             CastSpellByName("Jeeves")
  12.         end
  13.     else

or you could make a table instead:

lua Code:
  1. local EngineeringRepairItems = {
  2.     -- note these are random numbers,
  3.     -- not the actual item IDs.
  4.     123,
  5.     456,
  6.     789,
  7.     852,
  8.     159
  9. }

and then do:

lua Code:
  1. if RepairProfession == "Engineering" then
  2.         for _, id in next, EngineeringRepairItems do
  3.             if GetItemCount(id) > 0 and GetItemCooldown(id) == 0 then
  4.                 CastSpellByName(GetItemInfo(id))
  5.                 break
  6.             end
  7.         end
  8.     else

To work around the fact that you can't call CastSpellByName directly, your best bet is probably:

- Create an invisible secure action button
- When the user mouses over your data panel button, check for combat lockdown
- If you're not in combat, show the action button and reposition it to cover your data panel button
- When the user mouses away from the action button, or the user enters combat, hide the secure button and ClearAllPoints it (if you leave it anchored to an insecure button it will break)

cokedrivers 08-23-16 07:30 AM

Thanks for all the Help but for now I'm just going to use:
Lua Code:
  1. CastSpellByName("Traveler's Tundra Mammoth")
beings this is the only repair mount I currently have. Eventually I will probably add the Yak as well and when that time comes maybe there will be an easier way.

For right now here is my Duarability.lua file that goes with my cData addon,
Lua Code:
  1. local cData = LibStub("AceAddon-3.0"):GetAddon("cData")
  2.  
  3. ------------------------------------------------------------------------
  4. --   Durability Plugin Functions
  5. ------------------------------------------------------------------------
  6. cData.pluginConstructors["dur"] = function()
  7.  
  8.     db = cData.db.profile
  9.    
  10.     Slots = {
  11.         [1] = {1, "Head", 1000},
  12.         [2] = {3, "Shoulder", 1000},
  13.         [3] = {5, "Chest", 1000},
  14.         [4] = {6, "Waist", 1000},
  15.         [5] = {9, "Wrist", 1000},
  16.         [6] = {10, "Hands", 1000},
  17.         [7] = {7, "Legs", 1000},
  18.         [8] = {8, "Feet", 1000},
  19.         [9] = {16, "Main Hand", 1000},
  20.         [10] = {17, "Off Hand", 1000},
  21.         [11] = {18, "Ranged", 1000}
  22.     }
  23.  
  24.  
  25.     local plugin = CreateFrame('Frame', nil, Datapanel)
  26.     plugin:EnableMouse(true)
  27.     plugin:SetFrameStrata("MEDIUM")
  28.     plugin:SetFrameLevel(3)
  29.  
  30.     local Text  = plugin:CreateFontString(nil, "OVERLAY")
  31.     Text:SetFont(db.font, db.fontSize,'THINOUTLINE')
  32.     cData:PlacePlugin(db.dur, Text)
  33.  
  34.     local function OnEvent(self)
  35.         local Total = 0
  36.         local current, max
  37.        
  38.         for i = 1, 11 do
  39.             if GetInventoryItemLink("player", Slots[i][1]) ~= nil then
  40.                 current, max = GetInventoryItemDurability(Slots[i][1])
  41.                 if current then
  42.                     Slots[i][3] = current/max
  43.                     Total = Total + 1
  44.                 end
  45.             end
  46.         end
  47.         table.sort(Slots, function(a, b) return a[3] < b[3] end)
  48.        
  49.         if Total > 0 then
  50.             Text:SetText(hexa.."Armor: "..hexb..floor(Slots[1][3]*100).."% |r")
  51.         else
  52.             Text:SetText(hexa.."Armor: "..hexb.."100% |r")
  53.         end
  54.         -- Setup Durability Tooltip
  55.         self:SetAllPoints(Text)
  56.         Total = 0
  57.     end
  58.  
  59.     plugin:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
  60.     plugin:RegisterEvent("MERCHANT_SHOW")
  61.     plugin:RegisterEvent("PLAYER_ENTERING_WORLD")
  62.     plugin:SetScript("OnMouseDown",function(self,btn)
  63.         if btn == "LeftButton" then
  64.             ToggleCharacter("PaperDollFrame")
  65.         elseif btn == "RightButton" then
  66.             CastSpellByName("Traveler's Tundra Mammoth")
  67.         end
  68.     end)
  69.     plugin:SetScript("OnEvent", OnEvent)
  70.     plugin:SetScript("OnEnter", function(self)
  71.         if not InCombatLockdown() then
  72.             local anchor, panel, xoff, yoff = cData:DataTextTooltipAnchor(Text)
  73.             GameTooltip:SetOwner(panel, anchor, xoff, yoff)
  74.             GameTooltip:ClearLines()
  75.             GameTooltip:AddLine(hexa..PLAYER_NAME.."'s"..hexb.." Durability")
  76.             GameTooltip:AddLine' '         
  77.             for i = 1, 11 do
  78.                 if Slots[i][3] ~= 1000 then
  79.                     local green, red
  80.                     green = Slots[i][3]*2
  81.                     red = 1 - green
  82.                     GameTooltip:AddDoubleLine(Slots[i][2], floor(Slots[i][3]*100).."%",1 ,1 , 1, red + 1, green, 0)
  83.                 end
  84.             end
  85.         GameTooltip:AddLine(" ")
  86.         GameTooltip:AddLine("|cffeda55fLeft Click|r opens Character Panel.")
  87.         GameTooltip:AddLine("|cffeda55fRight Click|r summon's Traveler's Tundra Mammoth.")
  88.         GameTooltip:Show()
  89.         end
  90.     end)
  91.     plugin:SetScript("OnLeave", function() GameTooltip:Hide() end)
  92.  
  93.     return plugin -- important!
  94. end
Which will show up like the image below.


Thanks again Phanx for all the help you have given and still give to us not so good lua coders that want to make things our way.

Coke


All times are GMT -6. The time now is 01:49 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2019 MMOUI