Writing a (local) World of Warcraft Addon

Continuing down the wow addon / lua path, I figured now was a good time to write my first addon. I don't intend to publish this anywhere (except, well, here, kind-of); this is just for fun / a learning challenge.

Scenario / imagined need

Inventory Management is hard. I often jokingly say that IM is the REAL/hidden game within the game. It simply means that you tend to want to pick up way more stuff than your character can carry and have to make tough decisions on what to throw away, triggering your loss aversion and letting you experience the endowment effect.

Anyhow, to make this aspect of the game easier, I decided to write something heavily inspired by Scrap that would automatically sell gray/near-worthless items automatically while visiting a vendor. Why write my own? 'cause I'm a developer.

Structure

World of Warcraft Addons are subdirectories inside the Interface/Addons directory of your WoW installation (e.g. C:\Program Files (x86)\World of Warcraft\_classic_era_).

I decided to call my addon sxlScrap and therefore created a directory called just that. Inside my sxlScrap directory, WoW will look for a table of contents (.toc) file, where it expects to find references to all individual parts of my addon. For this simple case, my sxlScrap.toc file contains one line:


    main.lua

The script

Inside the main.lua file is the actual addon script. Here it is in all its glory:


    local BACKPACK_CONTAINER = 0
    local ITEM_QUALITY_POOR  = 0
    local frame = CreateFrame("Frame")

    frame:RegisterEvent('MERCHANT_SHOW')
    frame:SetScript("OnEvent", function()

        local numberOfBackpackSlots =
            GetContainerNumSlots(BACKPACK_CONTAINER)

        for slot = 0, numberOfBackpackSlots do
            local id = GetContainerItemID(BACKPACK_CONTAINER, slot)
            if id then
                local _, _, locked, quality, _, _, _ =
                    GetContainerItemInfo(BACKPACK_CONTAINER, slot)

                if not locked and quality == ITEM_QUALITY_POOR then
                    UseContainerItem(BACKPACK_CONTAINER, slot)
                end
            end
        end

    end)

What I'm hoping you'll get out of this, is to realize that there's not much to it and that you could do it too, if you wanted to. Granted, I understand that there's some cryptic stuff in there (like, "what's with all the underscores?"), but even so, the entire thing is just 23 lines (with many structures repeating). That's 8 google queries at most!

Learn more

The following articles were helpful while making this addon

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints