Thread Tools Display Modes
10-16-07, 10:05 AM   #1
Jeval
A Murloc Raider
Join Date: Oct 2007
Posts: 9
New to LUA and XML.. I could use some help.

I am trying to learn to write my own addons. LUA and XML will be my first programming languages. I got the book " Hacking World of Warcraft" that teaches you the basics of writing a custom UI for WoW. So far I have enjoyed it greatly. I just don’t seem to be able to get the code right though and was hoping someone out there could look over my code and tell me what I am doing wrong. I can't find a syntax error or anything but that doesn't mean there isn't one there. I am also using the SciTE editor. I am posting my code for the XML file and LUA file below. Please let me know if you can find where I am messing up. Thanks.


Whereabouts.lua code:

Code:
Whereabouts_Options = {
    show = true,
    lock = false,
    tooltip = true,
    worldmap = true,
    alpha = 1
};
function Whereabouts_OnLoad(self)
    SLASH_WHEREABOUTS1 = "/whereabouts";
    SLASH_WHEREABOUTS2 = "/wa";
    SlashCmdList["WHEREABOUTS"] = Whereabouts_SlashCommand;
end
function Whereabouts_SlashCommand(msg)
    msg = string.lower(msg);
    local args = {};
    for word in string.gmatch(msg, "[^%s]+") do
        table.insert(args, word);
    end
    if ( args[1] == "options" ) then
        Whereabouts_Options_Toggle();
    elseif ( args[1] == "show" ) then
        Whereabouts_Toggle();
    elseif ( args[1] == "lock" ) then
        Whereabouts_Options.lock = not Whereabouts_Options.lock;
    elseif ( args[1] == "tooltip" ) then
        Whereabouts_Options.tooltip = not Whereabouts_Options.tooltip;
    elseif ( arg[1] == "worldmap" ) then
        Whereabouts-WorldMap_Toggle();
    elseif ( arg[1] == "alpha" ) then
        local alpha = tonumber(args[2])
        if ( alpha and alpha >= 0 and alpha <= 1 ) then
            Whereabouts_Options.alpha = alpha;
            Whereabouts_Frame:SetAlpha(alpha) ;
        else
            Whereabouts_Message(WHEREABOUTS _OPTIONS_ALPHAWRONG) ;
        end
            else
                Whereabouts_Message(WHEREABOUTS_NOCOMMAND) ;
            end
        else
            Whereabouts_Message(WHEREABOUTS_USAGE);
            Whereabouts_Message("option - "..WHEREABOUTS_OPTIONS);
            Whereabouts_Message("show - "..WHEREABOUTS_OPTION_SHOW);
            Whereabouts_Message("lock - "..WHEREABOUTS_OPTION_LOCK);
            Whereabouts_Message("tooltip - "..WHEREABOUTS_OPTION_TOOLTIP);
            Whereabouts_Message("worldmap - "..WHEREABOUTS_OPTION_WOLRDMAP);
            Whereabouts_Message("alpha <#> - "..WHEREABOUTS_OPTION_ALPHA);
        end
    end
function Whereabouts_Toggle()
    if ( Whereabouts_Frame:IsShown() ) then
        Whereabouts_Frame:Hide();
    else
        Whereabouts_Frame:Show();
    end
end
local function round(num,  idp)
    local mult = 10 ^ (idp or 0)
    return math.floor(num * mult + 0.5) / mult
end
function Whereabouts_OnUpdate(self, elapsed)
    local x, y = GetPlayerMapPosition("player")
    local text
    if ( x == 0 and y == 0 ) then
        text = "--"
    else 
        x = round(x * 100, 2)
        y = round(y * 100, 2)
        text = string.format("%.2f, %.2f", x, y)
end
    Whereabouts_Frame_Text:SetText(text)
end
function Whereabouts_Message(msg)
    DEFAULT_CHAT_FRAME:AddMessage("[Whereabouts] "..msg) ;
end

Last edited by Jeval : 10-16-07 at 10:34 AM.
  Reply With Quote
10-16-07, 10:07 AM   #2
Jeval
A Murloc Raider
Join Date: Oct 2007
Posts: 9
And here is the XML code:

Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui..\FrameXML\UI.xsd">
    <Frame name="Whereabouts_Frame" toplevel="true"
    frameStrata="HIGH" enableMouse="true" movable="true"
    parent="UIParent" clampedToScreen="true">
        <Size>
            <AbsDimension x="100" y="32"/>
        </Size>
        <Backdrop edgeFile="Interface\Tooltips\UI-Tooltip-Border" bgFile="Interface\Tooltips\UI-Tooltip-Background">
            <BackgroundInsets>
                <AbsInset left="4" right="4" top="4" bottom="4"/>
            </BackgroundInsets>
            <EdgeSize>
                <AbsValue val="16"/>
            </EdgeSize>
            <Color r="0" g="0" b="0" a="0.4"/>
            <BorderColor r="1" g="0.8" b="0" a="0.8"/>
        </Backdrop>
        <Layers>
            <Layer level="OVERLAY">
                <FontString name="$parent_Text" inherits="GameFontNormal">
                    <Anchors>
                        <Anchor point="CENTER"/>
                    </Anchors>
                </FontString>
            </Layer>
        </Layers>
        <Scripts>
            <OnLoad>
                Whereabouts_OnLoad(self);
            </OnLoad>
            <OnUpdate>
                Whereabouts_OnUpdate(self, elapsed);
            </OnUpdate>
            <OnMouseDown>
                if ( button == "LeftButton" ) then
                    self:StartMoving();
                    self.isMoving = true;
                end
            </OnMouseDown>
            <OnMouseUp>
                if ( self.isMoving ) then
                    self:StopMovingOrSizing();
                    self.isMoving = false;
                end
            </OnMouseUp>
            <OnHide>
                if ( self.isMoving ) then
                    self:StopMovingOrSizing();
                    self.isMoving = false;
                end
            </OnHide>
        </Scripts>
    </Frame>
</Ui>
I will be thankful for any help anyone can give me.

Thanks,
Armok

Last edited by Jeval : 10-16-07 at 10:34 AM.
  Reply With Quote
10-17-07, 07:16 AM   #3
Eidolarr
An Aku'mai Servant
 
Eidolarr's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 34
I'd recommend grabbing BugSack and DevTools. BugSack will greatly ease your error tracking, and odds are right now you don't even have syntax errors set to show via default UI. DevTools will let you look at values, particularly tables, to see what's going on in your code as you play.

Generally if an addon doesn't work (does nothing) but doesn't generate any errors, there's a syntax issue in the XML preventing it from being interpreted, but by the look of it your Lua file is included via TOC, so that shouldn't matter. Can you post your TOC file?

Is any aspect of your addon functional? Does it appear on the addon list at login?

Oh, and as a little sidenote, local tables are generally a bad idea because when they are discarded they simply sit in memory as garbage. The same purpose could be served by using a global "temp" table, and such a table would be reused.
  Reply With Quote
10-17-07, 09:54 AM   #4
Jeval
A Murloc Raider
Join Date: Oct 2007
Posts: 9
Here is the TOC.

Code:
## Interface: 20003
## Title: Whereabouts
## Notes: Displays your coordinates in a movable window.
## SavedVariablesPerCharacter: Whereabouts_Options
enUS.lua
Whereabouts.lua
Whereabouts.xml
Whereabouts_Options.lua
Whereabouts_Options.xml
Whereabouts_WorldMap.xml
It does appear on the addons list. But is totally no functional in game. I get an error about the OnLoad and OnUpdate. I checked them both and cant seem to find where I made an error.

The gobal table and local tables you explained are still a little new to me. I have a basic understanding of how tables work and what they do but I am in the very very starting phases of learning this.

Thanks for your help.
  Reply With Quote
10-17-07, 10:11 AM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
1. current interface number is 20200
2. if you're getting errors, give us the error from BugSack.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
10-17-07, 11:47 AM   #6
Jeval
A Murloc Raider
Join Date: Oct 2007
Posts: 9
Originally Posted by Seerah
1. current interface number is 20200
2. if you're getting errors, give us the error from BugSack.
When I get home from work today I will post them. No WoW installed at work. LOL.
  Reply With Quote
10-17-07, 01:01 PM   #7
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Jeval
I am trying to learn to write my own addons. LUA and XML will be my first programming languages. I got the book " Hacking World of Warcraft" that teaches you the basics of writing a custom UI for WoW. So far I have enjoyed it greatly. I just don’t seem to be able to get the code right though and was hoping someone out there could look over my code and tell me what I am doing wrong. I can't find a syntax error or anything but that doesn't mean there isn't one there. I am also using the SciTE editor. I am posting my code for the XML file and LUA file below. Please let me know if you can find where I am messing up. Thanks.


Whereabouts.lua code:

Code:
Whereabouts_Options = {
    show = true,
    lock = false,
    tooltip = true,
    worldmap = true,
    alpha = 1
};
function Whereabouts_OnLoad(self)
    SLASH_WHEREABOUTS1 = "/whereabouts";
    SLASH_WHEREABOUTS2 = "/wa";
    SlashCmdList["WHEREABOUTS"] = Whereabouts_SlashCommand;
end
function Whereabouts_SlashCommand(msg)
    msg = string.lower(msg);
    local args = {};
    for word in string.gmatch(msg, "[^%s]+") do
        table.insert(args, word);
    end
    if ( args[1] == "options" ) then
        Whereabouts_Options_Toggle();
    elseif ( args[1] == "show" ) then
        Whereabouts_Toggle();
    elseif ( args[1] == "lock" ) then
        Whereabouts_Options.lock = not Whereabouts_Options.lock;
    elseif ( args[1] == "tooltip" ) then
        Whereabouts_Options.tooltip = not Whereabouts_Options.tooltip;
    elseif ( arg[1] == "worldmap" ) then
        Whereabouts-WorldMap_Toggle();
    elseif ( arg[1] == "alpha" ) then
        local alpha = tonumber(args[2])
        if ( alpha and alpha >= 0 and alpha <= 1 ) then
            Whereabouts_Options.alpha = alpha;
            Whereabouts_Frame:SetAlpha(alpha) ;
        else
            Whereabouts_Message(WHEREABOUTS _OPTIONS_ALPHAWRONG) ;
        end
            else
                Whereabouts_Message(WHEREABOUTS_NOCOMMAND) ;
            end
        else
            Whereabouts_Message(WHEREABOUTS_USAGE);
            Whereabouts_Message("option - "..WHEREABOUTS_OPTIONS);
            Whereabouts_Message("show - "..WHEREABOUTS_OPTION_SHOW);
            Whereabouts_Message("lock - "..WHEREABOUTS_OPTION_LOCK);
            Whereabouts_Message("tooltip - "..WHEREABOUTS_OPTION_TOOLTIP);
            Whereabouts_Message("worldmap - "..WHEREABOUTS_OPTION_WOLRDMAP);
            Whereabouts_Message("alpha <#> - "..WHEREABOUTS_OPTION_ALPHA);
        end
    end
function Whereabouts_Toggle()
    if ( Whereabouts_Frame:IsShown() ) then
        Whereabouts_Frame:Hide();
    else
        Whereabouts_Frame:Show();
    end
end
local function round(num,  idp)
    local mult = 10 ^ (idp or 0)
    return math.floor(num * mult + 0.5) / mult
end
function Whereabouts_OnUpdate(self, elapsed)
    local x, y = GetPlayerMapPosition("player")
    local text
    if ( x == 0 and y == 0 ) then
        text = "--"
    else 
        x = round(x * 100, 2)
        y = round(y * 100, 2)
        text = string.format("%.2f, %.2f", x, y)
end
    Whereabouts_Frame_Text:SetText(text)
end
function Whereabouts_Message(msg)
    DEFAULT_CHAT_FRAME:AddMessage("[Whereabouts] "..msg) ;
end
Whereabouts (Complete Addon) try comparing your code to that.
  Reply With Quote
10-17-07, 02:19 PM   #8
Eidolarr
An Aku'mai Servant
 
Eidolarr's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 34
Originally Posted by Jeval
The gobal table and local tables you explained are still a little new to me. I have a basic understanding of how tables work and what they do but I am in the very very starting phases of learning this.

Thanks for your help.
Ok, here's a little clarification. It's hard to explain if you haven't written in C or Java, but I'll try. Here are a few properties of tables.

Tables, unlike numbers and strings, are references. The implications of this are pretty heavy; consider this snippet:
Code:
local x = 5
local y = x
y = y - 3
print(x)
--will print 5
y is just a copy of x. If you used tables, however...
Code:
x = { }
x["value"] = 10
y = x
y["value"] = y["value"] - 3
print (x["value"])
--will print 7, not 10!
Take a second and think about that; x is not actually the table, it's just a arrow saying "look in this table if they use x!" When you say y = x, you're saying "make y point to the same table as x; because they're both pointing at the same table, changing one changes the other.

That was totally unrelated, but is quite a confusing topic... the sooner you get it down pat, the sooner you can be even more boggled by metatables!

To get back to local variables, tables are what the garbage collector reclaims. Basically in your function, every time you run it you create a new table. After that function is gone, the variable pointing to it is gone too; you can never access that again. The garbage collector goes through memory, finds all the things that are inaccessible (like your local table), and frees up the memory for other things. Garbage collection, however, takes resources, so the less garbage you generate, the better your performance will be. Because of that, if you just store one table outside of the function (IE, not local) and reuse it, you only use one table ever; you generate no garbage at all. Right now, in comparison, you keep generating new ones that become garbage after the function terminates.
  Reply With Quote
11-01-07, 03:57 PM   #9
jtlareau
A Defias Bandit
 
jtlareau's Avatar
Join Date: Nov 2007
Posts: 3
Jeval,

You had several typos and some lines of code that you missed when typing in the code from the book. I've attached updated files which correspond to code as it is supposed to be at the bottom of Page 276 (at the end of section "Writing Messages to the Chat Window".

Whereabouts.toc:
Code:
## Interface: 20200
## Title: Whereabouts
## Notes: Displays your coordinates in a movable window
## SavedVariablesPerCharacter: Whereabouts_Options
enUS.lua
Whereabouts.lua
Whereabouts.xml
Whereabouts_Options.lua
Whereabouts_Options.xml
Whereabouts_Worldmap.xml
Whereabouts.xml:
Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui..\FrameXML\UI.xsd">
    <Frame name="Whereabouts_Frame" toplevel="true" frameStrata="HIGH" enableMouse="true" movable="true" parent="UIParent" clampedToScreen="true">
        <Size>
            <AbsDimension x="100" y="32"/>
        </Size>
		<Anchors>
			<Anchor point="CENTER"/>
		</Anchors>
        <Backdrop edgeFile="Interface\Tooltips\UI-Tooltip-Border" bgFile="Interface\Tooltips\UI-Tooltip-Background">
            <BackgroundInsets>
                <AbsInset left="4" right="4" top="4" bottom="4"/>
            </BackgroundInsets>
            <EdgeSize>
                <AbsValue val="16"/>
            </EdgeSize>
            <Color r="0" g="0" b="0" a="0.4"/>
            <BorderColor r="1" g="0.8" b="0" a="0.8"/>
        </Backdrop>
        <Layers>
            <Layer level="OVERLAY">
                <FontString name="$parent_Text" inherits="GameFontNormal">
                    <Anchors>
                        <Anchor point="CENTER"/>
                    </Anchors>
                </FontString>
            </Layer>
        </Layers>
        <Scripts>
            <OnLoad>
                Whereabouts_OnLoad(self);
            </OnLoad>
            <OnUpdate>
                Whereabouts_OnUpdate(self, elapsed);
            </OnUpdate>
            <OnMouseDown>
                if ( button == "LeftButton" ) then
                    self:StartMoving();
                    self.isMoving = true;
                end
            </OnMouseDown>
            <OnMouseUp>
                if ( self.isMoving ) then
                    self:StopMovingOrSizing();
                    self.isMoving = false;
                end
            </OnMouseUp>
            <OnHide>
                if ( self.isMoving ) then
                    self:StopMovingOrSizing();
                    self.isMoving = false;
                end
            </OnHide>
        </Scripts>
    </Frame>
</Ui>
Whereabouts.lua:
Code:
Whereabouts_Options = {
    show = true,
    lock = false,
    tooltip = true,
    worldmap = true,
    alpha = 1
};

function Whereabouts_OnLoad(self)
    SLASH_Whereabouts1 = "/Whereabouts";
    SLASH_Whereabouts2 = "/wa";
    SlashCmdList["Whereabouts"] = Whereabouts_SlashCommand;
end

function Whereabouts_SlashCommand(msg)
    msg = string.lower(msg);
    local args = {};
    for word in string.gmatch(msg, "[^%s]+") do
        table.insert(args, word);
    end
	if ( args[1] ) then
		if ( args[1] == "options" ) then
			Whereabouts_Options_Toggle();
		elseif ( args[1] == "show" ) then
			Whereabouts_Toggle();
		elseif ( args[1] == "lock" ) then
			Whereabouts_Options.lock = not Whereabouts_Options.lock;
		elseif ( args[1] == "tooltip" ) then
			Whereabouts_Options.tooltip = not Whereabouts_Options.tooltip;
		elseif ( arg[1] == "worldmap" ) then
			Whereabouts_WorldMap_Toggle();
		elseif ( arg[1] == "alpha" ) then
			local alpha = tonumber(args[2])
			if ( alpha and alpha >= 0 and alpha <= 1 ) then
				Whereabouts_Options.alpha = alpha;
				Whereabouts_Frame:SetAlpha(alpha) ;
			else
				Whereabouts_Message(WHEREABOUTS_OPTIONS_ALPHAWRONG) ;
			end
        else
            Whereabouts_Message(WHEREABOUTS_USAGE);
            Whereabouts_Message("option - "..WHEREABOUTS_OPTIONS);
            Whereabouts_Message("show - "..WHEREABOUTS_OPTION_SHOW);
            Whereabouts_Message("lock - "..WHEREABOUTS_OPTION_LOCK);
            Whereabouts_Message("tooltip - "..WHEREABOUTS_OPTION_TOOLTIP);
            Whereabouts_Message("worldmap - "..WHEREABOUTS_OPTION_WOLRDMAP);
            Whereabouts_Message("alpha <#> - "..WHEREABOUTS_OPTION_ALPHA);
		end
    end	
end
	
function Whereabouts_Toggle()
    if ( Whereabouts_Frame:IsShown() ) then
        Whereabouts_Frame:Hide();
    else
        Whereabouts_Frame:Show();
    end
end

local function round(num,  idp)
    local mult = 10 ^ (idp or 0)
    return math.floor(num * mult + 0.5) / mult
end

function Whereabouts_OnUpdate(self, elapsed)
    local x, y = GetPlayerMapPosition("player")
    local text
    if ( x == 0 and y == 0 ) then
        text = "--"
    else 
        x = round(x * 100, 2)
        y = round(y * 100, 2)
        text = string.format("%.2f, %.2f", x, y)
	end
    Whereabouts_Frame_Text:SetText(text)
end

function Whereabouts_Message(msg)
    DEFAULT_CHAT_FRAME:AddMessage("[Whereabouts] "..msg) ;
end
enUS.lua:
Code:
WHEREABOUTS_NOCOMMAND = "That's not an option, type \"/wa\" for help";
WHEREABOUTS_OPTIONS = "Toggle the options panel";
WHEREABOUTS_OPTIONS_ALPHA = "Set the transparency of the window";
WHEREABOUTS_OPTIONS_ALPHAWRONG = "Alpha must be between 0 and 1";
WHEREABOUTS_OPTIONS_LOCK = "Lock the coordinates window in place";
WHEREABOUTS_OPTIONS_SHOW = "Show the coordinates window";
WHEREABOUTS_OPTIONS_TOOLTIP = "Enable the tooltip display";
WHEREABOUTS_OPTIONS_WORLDMAP = "Enable coordinates on the worldmap display";
WHEREABOUTS_USAGE = "Usage: /whereabouts <option> or /wa <option>";
Hope this helps...

Jim Lareau
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » New to LUA and XML.. I could use some help.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off