A Strong Foundation

It’s time to create some code and make something work.

World of Warcraft AddOns are event driven. That means that the game informs them when something happens and the AddOns can choose to perform actions in response.

Events are sent to frames and since events are at the core of how everything works, we will actually base all of our code around a single frame by creating that frame and then adding class methods to it.

First we create our frame. A frame is bit like the equivalent of a window in Windows. It’s a core UI element that can be a window, a button, or an image etc. Our frame won’t be displayed on-screen but it will be hidden away in the background, receiving and responding to game events.

PortalWhere = CreateFrame("Frame")

Then we define a new method called Print which accepts a varying number of arguments and passes them all to the WoW’s built-in print function after prepending [PortalWh*re] to the text. This isn’t necessary but it’s nice to know which AddOn printed text to the chat box.

function PortalWhere:Print(...)
    print("[PortalWh*re] " .. ...)
end

The next method we define is Boot. Here we call SetScript to tell the frame how we’d like to handle received events. We pass a lambda function to it which which cleverly looks at the name of the event we’re receiving and will call a method on our class with the exact same name and pass all of the arguments to it.

function PortalWhere:Boot()
    self:SetScript("OnEvent", function(self, event, ...)
        self[event](self, ...)
    end)
    self:RegisterEvent("ADDON_LOADED")
end

We then also call RegisterEvent to let the game know that we want to be informed about the ADDON_LOADED event. This is an event that’s fired every time an AddOn has finished loading. It’ll fire once for every loaded AddOn and we’re going to use this as a place to start our initialisation.

function PortalWhere:ADDON_LOADED(name)
    if name == "PortalWhere" then
        self:OnBoot()
    end
end

function PortalWhere:OnBoot()
    self:Print("Loaded.")
end

Next we define the ADDON_LOADED method which will be called automagically by our little lambda call that we passed to SetScript. It receives the name of the AddOn that just loaded. We’ll make sure it was us that loaded and if so we call OnBoot, which (for now) just prints a message to the console to show that our freshly created AddOn has been successfully loaded.

Then all we need to do to get things going is actually call our Boot method. The Core.lua file now looks like this:

PortalWhere = CreateFrame("Frame")

function PortalWhere:Print(...)
    print("[PortalWh*re]", ...)
end

function PortalWhere:Boot()
    self:SetScript("OnEvent", function(self, event, ...)
        self[event](self, ...)
    end)
    self:RegisterEvent("ADDON_LOADED")
end

function PortalWhere:ADDON_LOADED(name)
    if name == "PortalWhere" then
        self:OnBoot()
    end
end

function PortalWhere:OnBoot()
    self:Print("Loaded.")
end

PortalWhere:Boot()

Congratulations, you have a working AddOn. Sure, it’s not very useful right now but it’s a strong foundation to build on.

Last modified: June 22, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.