-
Posted by mager1794 on Fri, 18 Mar 2011 11:25:35
I'm new to AddOns but i'm a fairly skilled programmer and i am having a lot of problems with creating this first simple addon.
<Ui xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.blizzard.com/wow/ui/"> <Script file="Frame.lua" /> <Frame name="Frame1"> <Size> <AbsDimension x="242" y="125" /> </Size> <Anchors> <Anchor point="TOPLEFT"> <Offset x="174" y="-53" /> </Anchor> </Anchors> <Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true"> <BackgroundInsets> <AbsInset left="11" right="12" top="12" bottom="11" /> </BackgroundInsets> <TileSize> <AbsValue val="32" /> </TileSize> <EdgeSize> <AbsValue val="32" /> </EdgeSize> </Backdrop> <Scripts> <OnLoad>Frame1_OnLoad();</OnLoad> <OnEvent>Frame1_OnEvent();</OnEvent> </Scripts> </Frame> </Ui>
Lua
function Frame1_OnLoad() print("Target Name Addon!"); this:RegisterEvent("PLAYER_TARGET_CHANGED"); this:RegisterEvent("CHAT_MSG_MONEY"); end function Frame1_OnEvent() if (event == "PLAYER_TARGET_CHANGED") then print("Hello " .. UnitName("target") .. "!"); end print("Hello world!"); end
The problem is that my events do not trigger at all. I've tried everything i can think of and they do not. Could someone point me into the right direction?
-
Posted by jnwhiteh on Fri, 18 Mar 2011 12:17:39
I'm new to AddOns but i'm a fairly skilled programmer and i am having a lot of problems with creating this first simple addon.
There's a few problems and bad practices being used in this.. is there a reason you're not using the examples and styles that I present in the book? If you follow that style, you won't go wrong.
<Ui xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.blizzard.com/wow/ui/"> <Script file="Frame.lua" />
There's no need to load the lua script here, you should do it in the .toc file instead just so you only have one place to look and see when a file is being loaded.
<Frame name="Frame1"> <Size> <AbsDimension x="242" y="125" /> </Size> <Anchors> <Anchor point="TOPLEFT"> <Offset x="174" y="-53" /> </Anchor> </Anchors> <Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true"> <BackgroundInsets> <AbsInset left="11" right="12" top="12" bottom="11" /> </BackgroundInsets> <TileSize> <AbsValue val="32" /> </TileSize> <EdgeSize> <AbsValue val="32" /> </EdgeSize> </Backdrop> <Scripts> <OnLoad>Frame1_OnLoad();</OnLoad>
This should be changed to
<OnLoad>Frame1_OnLoad(self)</OnLoad>
, since you need to pass the argument to the frame handler to your sub-function.<OnEvent>Frame1_OnEvent();</OnEvent>
Same issue here, except the signature is more complicated. This needs to be
<OnEvent>Frame1_OnEvent(self, event, ...)</OnEvent>
.</Scripts> </Frame> </Ui>
Lua
function Frame1_OnLoad() print("Target Name Addon!"); this:RegisterEvent("PLAYER_TARGET_CHANGED"); this:RegisterEvent("CHAT_MSG_MONEY"); end
The global variable
this
no longer exists and hasn't since WoTLK. You are almost certainly getting a Lua error on one of these two lines telling you thatthis
is a nil value. If not, you should enabled Lua errors (Interface Options -> Help -> Display Lua Errors). In order to make this work, you need to change the signature to match what is expected for OnLoads, i.e. make the following changes:function Frame1_OnLoad(self) print("Target Name Addon!"); self:RegisterEvent("PLAYER_TARGET_CHANGED"); self:RegisterEvent("CHAT_MSG_MONEY"); end
You've got the same problem in this event function, it needs to be something like this:
function Frame1_OnEvent() if (event == "PLAYER_TARGET_CHANGED") then print("Hello " .. UnitName("target") .. "!"); end print("Hello world!"); end
function Frame1_OnEvent(self, event, ...) if (event == "PLAYER_TARGET_CHANGED") then print("Hello " .. UnitName("target") .. "!"); end print("Hello world!"); end
The problem is that my events do not trigger at all. I've tried everything i can think of and they do not. Could someone point me into the right direction?
The issue is actually before that, but hopefully this points you in the right direction.