Fandom

WoWWiki

Lua Addon programming

  • I'm having a problem with my code. So far everything loads fine except the values I'm pushing. And only at the start. When I reload the ui, it inputs the proper values and I can continue on with my day. I end up with unitXp (at the start) and maxXp showing up as 0. And when I reload the ui, it's like, "oh yeah, shit. forgot these. here ya go", then it's all good. I'm no stranger to object oriented programming, however I've never used lua before. HELP APPRECIATED

    local unitXp = UnitXP("player") -- Returns the number of experience points the specified unit has in their current level. (only works on your player)
    local maxXp = UnitXPMax("player") -- Returns the number of experience points the specified unit needs to reach their next level. (only works on your player)
    local restState = GetRestState() -- Returns information about a player's rest state (saved up experience bonus)
    local xpDisabled = IsXPUserDisabled() -- Returns 1 if the character has disabled experience gain.
    local petXp = GetPetExperience() -- Returns the pet's current xp, and total xp required for next level.
    local xpExhaustion = GetXPExhaustion() -- Returns your character's current rested XP, nil if character is not rested.
    local debugtrue = false
    
    
    local oldxp = 0
    local pass1 = true
    
    
    print("======================================")
    print("LOADED INFORMATION")
    print("asdf:" .. unitXp)
    print("asdf:" .. maxXp)
    print("asdf:" .. petXp)
    print("asdf:" .. restState)
    print(xpDisabled)
    print(xpExhaustion)
    print("======================================")
    
    
    
    
    --sets the oldxp to current xp once at beginning of addon
    local function setXp()
    if pass1 then
    oldxp = unitXp
    pass1 = false
    print("if you see me more than once, we got problems")
    end
    end
    
    
    setXp()
    
    
    
    
    
    
    --returns the number of enemies to kill, of last kill, to gain new level
    local function killToLevel()
    if debugtrue then
    print(maxXp .. " maxxp")
    print(unitXp .. " unitXp")
    print(oldxp .. " oldxp")
    end
    local xpAmt = unitXp - oldxp
    if xpAmt > 1 then
    local killnum = (maxXp - unitXp)/(unitXp - oldxp)
    oldxp = unitXp
    return ceil(killnum)
    else
    return 0
    end
    end
    
    
    local MyAddon = CreateFrame("frame","MyAddonFrame")
    MyAddon:SetBackdrop({
        bgFile="Interface\\DialogFrame\\UI-DialogBox-Background", 
        edgeFile="Interface\\DialogFrame\\UI-DialogBox-Border", 
        tile=1, tileSize=32, edgeSize=32, 
        insets={left=11, right=12, top=12, bottom=11}
    })
    windowWidth = 300
    windowHeight = 75
    MyAddon:SetWidth(windowWidth)
    MyAddon:SetHeight(windowHeight)
    MyAddon:SetPoint("CENTER",UIParent)
    MyAddon:EnableMouse(true)
    MyAddon:SetMovable(true)
    MyAddon:RegisterForDrag("LeftButton")
    MyAddon:SetScript("OnDragStart", function(self) self:StartMoving() end)
    MyAddon:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
    MyAddon:SetFrameStrata("FULLSCREEN_DIALOG")
    
    
    local experienceA = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    local experience1 = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    local experienceB = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    local experience2 = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    local experienceC = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    local experience3 = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    
    
    print(killnum)
    
    
    local function draw()
    if debugtrue then
    print("debugging")
    end
    
    
    experienceA:SetPoint("TOPLEFT", 15, -15)
    experienceA:SetFormattedText("Experience: ")
    
    
    experience1:SetPoint("TOPRIGHT", -15, -15)
    experience1:SetFormattedText(unitXp .. " / " .. maxXp)
    
    
    experienceB:SetPoint("TOPLEFT", 15, -30)
    experienceB:SetFormattedText("Extra rest XP:  ")
    
    
    experience2:SetPoint("TOPRIGHT", -15, -30)
    experience2:SetFormattedText("None")
    if xpExhaustion then
    print("here 1")
    experience2:SetFormattedText(xpExhaustion)
    else<span class="Apple-tab-span" style="white-space:pre"> </span>
    print("here 2")
    experience2:SetFormattedText("None")
    end
    
    
    experienceC:SetPoint("TOPLEFT", 15, -45)
    if not restState then
    experienceC:SetFormattedText("You are rested.")
    else
    experienceC:SetFormattedText("You are not rested.")
    
    
    experience3:SetPoint("RIGHT", -15, -15)
    experience3:SetFormattedText(killToLevel())
    
    
    end
    end
    
    
    draw()
    
    
    --updates variables when called
    local function updateVariables()
    if debugtrue then
    print("updatevariables function")
    end
    unitXp = UnitXP("player")
    maxXp = UnitXPMax("player")
    restState = GetRestState()
    xpDisabled = IsXPUserDisabled()
    petXp = GetPetExperience()
    xpExhaustion = GetXPExhaustion()
    draw()
    end
    
    
    updateVariables()
    
    
    --Updates frames when XP is gained
    local frame = CreateFrame("FRAME", "FooAddonFrame");
    frame:RegisterEvent("PLAYER_XP_UPDATE");
    local function eventHandler(self, event, ...)
    if debugtrue then
    print("eventHandler xp update function")
    end
    updateVariables()
    print(killnum .. ": after updateVariables")
    end
    frame:SetScript("OnEvent", eventHandler);
    
      Loading editor
Give Kudos to this message
You've given this message Kudos!
See who gave Kudos to this message

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.