Thread Tools Display Modes
12-27-10, 05:58 PM   #1
stormszero
A Defias Bandit
Join Date: Dec 2010
Posts: 2
Problems with Events

Hi,

I've decided to dig deeper into the world of addons for WoW and in that regard I've been looking for resources albeit detailed instructions seem to be somewhat scarce.

I've gotten a nice little window frame created which will suit my need quite well, but in order to really get going I need to register some events, but somehow I can't get them to fire. It's very possible I'm overlooking something which'd be blindingly obvious, but I seem to have stared too long and hard at it.

The setup is pretty simple. I have cut parts for readability but shout out if I have omitted key information.

First, the XML file:

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">
	<Script File="HMana.lua"/>
	<Frame name="hManaFrame" toplevel="true" parent="UIParent" frameStrata="DIALOG" hidden="false" enableMouse="true" movable="true">
...
		<Scripts>
			<OnLoad>
				hMana_OnLoad();
			</OnLoad>
			<OnEvent>
				hMana_OnEvent();
			</OnEvent>
...
And the LUA file, again cutted for readability:

Code:
function hMana_OnLoad()
	print("registering event");
	hManaFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
	hManaFrame:RegisterEvent("PLAYER_TARGET_CHANGED");
	hManaFrame:RegisterEvent("LOOT_OPENED");
	print("done registering events");
end
...
function hMana_OnEvent()
	if (event == "PLAYER_ENTERING_WORLD") then
		print("Test. Entered world.");
	else
		print("Just a test, event fired: " .. event);
	end
end
I can with the print's in the OnLoad function tell that it indeed does get called, and supposedly the three events should be bound to the frame which I've defined in the XML file. Am I wrong here?

Right or wrong, it doesn't seem like the OnEvent function ever gets called (some output should appear in the chatframe under any circumstances when the OnEvent function gets executed) and I'm clueless as to why. I'd really love if any of you could correct me me where and if I'm wrong.

Best regards,
Klaus
  Reply With Quote
12-27-10, 06:39 PM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,935
They recently changed how the OnEvent function, amongst others, work.

You want to explicitly specify the parameters of the wow functions so that the addon knows what you are talking about.

EG.

For OnEvent that would be something as follows:

Code:
local function OnEvent(self,event,...)
   local arg1,arg2,arg3,arg4,arg5 = ...      -- and so on and so forth

   if event == "MY EVENT TO CHECK" then
      print(arg1,arg2,arg3)
   elseif event == "ANOTHER EVENT TO CHECK" then
      print(arg1,arg2)
   end
end
Other than that I can't see anything glaring out at me that could be causing the effect you are having.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
12-28-10, 04:44 AM   #3
stormszero
A Defias Bandit
Join Date: Dec 2010
Posts: 2
Hello again,

Thank you for the advice. I continued to struggle with the addon and decided to can it and redo it from scratch, and now the OnEvent handler detects events, although the available data seems to be absent. I have the following handler:

Code:
function Test_OnEvent(self, event, ...)
	arg1, arg2, arg3, arg4 = ...
	if (event == "PLAYER_TARGET_CHANGED") then
		print("caught PLAYER_TARGET_CHANGED: " .. arg1)
	else
		print(arg1)
	end
end
I've registered the event PLAYER_TARGET_CHANGED in my OnLoad, which binds it to the TestFrame declared in my XML file:

Code:
function Test_OnLoad()
	TestFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
end
Now, as far as I can understand PLAYER_TARGET_CHANGED fires whenever you click a character, npc or it deselects for any reason. However, when I click an NPC in orgrimmar, it just prints "nil" to the default chat frame (which happens because the content of "event" is nil); If I attempt to output "event" (i.e. print(event)) or output the various argX variables, they're all nils.

Again, I am new to LUA but I do understand the concepts from other languages, but this is a completely new syntax for me, I'm used to C-style languages, so forgive me if I am literally standing on the answer

/Klaus
  Reply With Quote
12-28-10, 09:27 AM   #4
Chamenas
Premium Member
 
Chamenas's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 14
Originally Posted by stormszero View Post
Hello again,

Thank you for the advice. I continued to struggle with the addon and decided to can it and redo it from scratch, and now the OnEvent handler detects events, although the available data seems to be absent. I have the following handler:

Code:
function Test_OnEvent(self, event, ...)
	arg1, arg2, arg3, arg4 = ...
	if (event == "PLAYER_TARGET_CHANGED") then
		print("caught PLAYER_TARGET_CHANGED: " .. arg1)
	else
		print(arg1)
	end
end
I've registered the event PLAYER_TARGET_CHANGED in my OnLoad, which binds it to the TestFrame declared in my XML file:

Code:
function Test_OnLoad()
	TestFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
end
Now, as far as I can understand PLAYER_TARGET_CHANGED fires whenever you click a character, npc or it deselects for any reason. However, when I click an NPC in orgrimmar, it just prints "nil" to the default chat frame (which happens because the content of "event" is nil); If I attempt to output "event" (i.e. print(event)) or output the various argX variables, they're all nils.

Again, I am new to LUA but I do understand the concepts from other languages, but this is a completely new syntax for me, I'm used to C-style languages, so forgive me if I am literally standing on the answer

/Klaus
You're not showing us all we need to know, because I don't see a call to Test_OnEvent, which would show us why it's doing what you're reporting. My assumption is that you're not properly calling the function when the event fires.

I don't really deal with XML in my AddOns, so it's difficult for me to know exactly how to help in this situation. However, if you come from a C-style background, you might find you want to work exclusively in Lua as opposed to a Lua-XML approach. Some are more comfortable combining both and some are more comfortable just using Lua. In Lua, what you are trying to do would look something like:

Code:
local function Test_OnEvent(self, event, ...)
	arg1, arg2, arg3, arg4 = ...
	if (event == "PLAYER_TARGET_CHANGED") then
		print("caught PLAYER_TARGET_CHANGED: " .. arg1)
	else
		print(arg1)
	end
end

local eventFrame = CreateFrame("FRAME")
eventFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
eventFrame:SetScript("OnEvent", Test_OnEvent)
I would highly recommend using the following resources:
WoWPedia.org :: Interface
WoWProgramming.com (There's even a book you can buy and the author is quite active in the community)
lua.org (Lua documentation)
  Reply With Quote
12-28-10, 09:30 AM   #5
Beoko
Guest
Posts: n/a
I recommend using pure Lua in circumstances where XML is not required (which is all of perhaps two situations I can think about offhand).

lua Code:
  1. local Frame = CreateFrame("Frame")
  2. Frame:RegisterEvent("PLAYER_TARGET_CHANGED")
  3. Frame:SetScript("OnEvent", function() print("This actually works.") end)
  Reply With Quote
12-28-10, 08:47 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by stormszero View Post
Now, as far as I can understand PLAYER_TARGET_CHANGED fires whenever you click a character, npc or it deselects for any reason. However, when I click an NPC in orgrimmar, it just prints "nil" to the default chat frame (which happens because the content of "event" is nil); If I attempt to output "event" (i.e. print(event)) or output the various argX variables, they're all nils.

Again, I am new to LUA but I do understand the concepts from other languages, but this is a completely new syntax for me, I'm used to C-style languages, so forgive me if I am literally standing on the answer

/Klaus
Originally Posted by stormszero View Post
First, the XML file:
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">
	<Script File="HMana.lua"/>
	<Frame name="hManaFrame" toplevel="true" parent="UIParent" frameStrata="DIALOG" hidden="false" enableMouse="true" movable="true">
...
		<Scripts>
			<OnLoad>
				hMana_OnLoad();
			</OnLoad>
			<OnEvent>
				hMana_OnEvent();
			</OnEvent>
...
Right now, your XML file isn't passing any of the parameters for the event handlers. Using the old style is tricky since it wraps the handler with a function that has prenamed parameters. You can bypass this by using the function="func" attribute for the script tag.

Here's an example.
Code:
<OnLoad function="hMana_OnLoad"/>
<OnEvent function="hMana_OnEvent"/>
Note this is not a function call, it's simply grabbing the pointer to the function and setting the script handler to it. The function MUST EXIST at the time you make this assignment otherwise the handler will never run (the function referenced will be nil).
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
12-29-10, 02:20 AM   #7
Chamenas
Premium Member
 
Chamenas's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 14
Originally Posted by Beoko View Post
I recommend using pure Lua in circumstances where XML is not required (which is all of perhaps two situations I can think about offhand).

lua Code:
  1. local Frame = CreateFrame("Frame")
  2. Frame:RegisterEvent("PLAYER_TARGET_CHANGED")
  3. Frame:SetScript("OnEvent", function() print("This actually works.") end)
It's really about what the user is comfortable with. If they're comfortable with XML and Lua, then why stop them? Though, in this case, I feel that the original poster will find themselves far more comfortable with Lua.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Problems with Events


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