Tag: command

  • Create Your First Slash Command in WoW Classic (/hello)

    Create Your First Slash Command in WoW Classic (/hello)

    In the first part of this series, we built a tiny addon that prints a message when you log in. Now we’ll make it a bit more interactive by adding your first slash command: /hello.

    When you type /hello in chat, the addon will greet you by name. This is often the next step for new addon authors because slash commands are a simple way to trigger your code on demand.

    What Is a Slash Command?

    Slash commands are those chat commands that start with /:

    • /reload
    • /dance
    • /who
    • …and of course, custom ones like /hello.

    Addons can register their own commands and link them to Lua functions. When you type the command in chat, WoW calls the function you defined.

    Reusing the Same Addon Folder

    We’ll continue using the same addon from part 1:

    World of Warcraft\_classic_\Interface\AddOns\MyFirstAddon\

    You should already have:

    • MyFirstAddon.toc
    • main.lua

    We don’t need to change the TOC file for this step. All the magic will happen inside main.lua.

    Adding a /hello Command

    Open main.lua and start from what we had before:

    local frame = CreateFrame("FRAME")
    frame:RegisterEvent("PLAYER_LOGIN")
    
    frame:SetScript("OnEvent", function()
        print("|cfff4b034MyFirstAddon loaded successfully!|r")
    end)

    Now we’ll add a simple function and register a slash command.

    1. Write the Hello Function

    Add this below the existing code:

    local function MyFirstAddon_SayHello(msg)
        local playerName = UnitName("player")
    
        if msg and msg ~= "" then
            print("|cfff4b034[MyFirstAddon]|r Hello, " .. playerName .. "! You said: " .. msg)
        else
            print("|cfff4b034[MyFirstAddon]|r Hello, " .. playerName .. "!")
        end
    end

    What it does:

    • Gets your character name with UnitName("player").
    • If you type extra text after /hello, it prints that too. Example: /hello nice UI → “You said: nice UI”.

    2. Register the Slash Command

    WoW uses a global table called SLASH_<NAME><number> to connect slash commands with a handler function named SlashCmdList["NAME"].

    Add this under the function you just created:

    SLASH_MYFIRSTADDON1 = "/hello"
    
    SlashCmdList["MYFIRSTADDON"] = function(msg)
        MyFirstAddon_SayHello(msg)
    end

    This means:

    • /hello is now a valid command.
    • Whenever you type /hello, WoW calls the function registered in SlashCmdList["MYFIRSTADDON"].
    • We forward the message (msg) to MyFirstAddon_SayHello.

    Final main.lua Example

    Here’s the complete file so you can compare:

    -- Create a frame to listen to events
    local frame = CreateFrame("FRAME")
    frame:RegisterEvent("PLAYER_LOGIN")
    
    -- When the player logs in, print a message
    frame:SetScript("OnEvent", function()
        print("|cfff4b034MyFirstAddon loaded successfully! Type /hello to test it.|r")
    end)
    
    -- Our hello function
    local function MyFirstAddon_SayHello(msg)
        local playerName = UnitName("player")
    
        if msg and msg ~= "" then
            print("|cfff4b034[MyFirstAddon]|r Hello, " .. playerName .. "! You said: " .. msg)
        else
            print("|cfff4b034[MyFirstAddon]|r Hello, " .. playerName .. "!")
        end
    end
    
    -- Register the /hello slash command
    SLASH_MYFIRSTADDON1 = "/hello"
    
    SlashCmdList["MYFIRSTADDON"] = function(msg)
        MyFirstAddon_SayHello(msg)
    end

    Testing Your /hello Command In-Game

    1. Save main.lua.
    2. (Re)start WoW Classic or type /reload in-game.
    3. On login, you should see:

    MyFirstAddon loaded successfully! Type /hello to test it.

    4. In the chat window, type:

    • /hello
    • /hello this is my first addon

    You should see your greeting messages printed back to you.

    Troubleshooting

    • “Interface action failed because of an AddOn”
      There’s likely a syntax error. Check missing quotes, commas, or end.
    • /hello does nothing
      Make sure MyFirstAddon is enabled in the AddOns menu, and check that the slash name matches exactly: SLASH_MYFIRSTADDON1 and SlashCmdList["MYFIRSTADDON"].

    Where We Go From Here

    You now have:

    • A loading message on PLAYER_LOGIN
    • Your first custom slash command
    • A small reusable function (MyFirstAddon_SayHello)

    Next possible steps:

    • Add /hello help with a small help text
    • Register multiple commands (/hello and /hi)
    • Start creating a simple UI frame to display options instead of only using chat

    Stay tuned — your addon is starting to become interactive, and this is just the beginning of what you can build for your own WoW Classic UI!