Thread Tools Display Modes
03-07-10, 12:04 AM   #1
Zinc
A Deviate Faerie Dragon
Join Date: Mar 2010
Posts: 13
Double Events

Alright, I`m no stranger to coding, but I`m a bit new to WoW API Lua. So, for my first challenge, I`m making a chat bot. So, I decided to make it have a welcoming message. This is what I had so far.

Code:
  local frame = CreateFrame("FRAME", "MParent");
  frame:RegisterEvent("CHAT_MSG_CHANNEL_JOIN");

  local function autoResponse()
    print("|CFF00FF00Someone has entered the channel.|R");
  end

  frame:SetScript("OnEvent",autoResponse);
However, it is firing the event twice each time someone joins the channel, so it says:

Someone has entered the channel.
Someone has entered the channel.
  Reply With Quote
03-07-10, 03:09 AM   #2
Sekrin
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 150
Have you tried dumping out the arguments from the event?

You could either add some code to your existing addon to do this, or you might like to use the eventtrace tool (/eventtrace).
  Reply With Quote
03-07-10, 08:10 AM   #3
Zinc
A Deviate Faerie Dragon
Join Date: Mar 2010
Posts: 13
Originally Posted by Sekrin View Post
Have you tried dumping out the arguments from the event?

You could either add some code to your existing addon to do this, or you might like to use the eventtrace tool (/eventtrace).
No idea how to do that.
  Reply With Quote
03-07-10, 10:19 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
How do you load your posted code ?
I've seen cases where it was loaded twice resulting in double events.
Is it loaded in the toc and in a xml ?
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
03-07-10, 10:57 AM   #5
Zinc
A Deviate Faerie Dragon
Join Date: Mar 2010
Posts: 13
.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">
 
  <Script File="HelloWorld.lua"/>
  <Frame name="MParent">
  </Frame>

</Ui>
.lua
Code:
  local frame = CreateFrame("FRAME", "MParent");
  frame:RegisterEvent("CHAT_MSG_CHANNEL_JOIN");

  local function autoResponse()
    print("|CFF00FF00Someone has entered the channel.|R");
  end

  frame:SetScript("OnEvent",autoResponse);
.toc
Code:
## Interface: 30100
## Title: Hello World!
## Notes: All Purpose
## Dependencies: 
HelloWorld.lua
HelloWorld.xml
As you can see, I just modify one AddOn to serve as all my AddOns.

Last edited by Zinc : 03-07-10 at 11:03 AM.
  Reply With Quote
03-07-10, 11:14 AM   #6
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
You're loading HelloWorld.lua twice, once in the .toc and once in the .xml. Ergo, you get double messages.
__________________
Oh, the simulated horror!
  Reply With Quote
03-07-10, 11:16 AM   #7
Zinc
A Deviate Faerie Dragon
Join Date: Mar 2010
Posts: 13
So should I remove the .lua as a dependency in my .toc?

Edit: Or should I remove '<Script File="HelloWorld.lua"/>' from .xml?

Editedit: I removed the line from the first edit and it works now, thanks guys!

Editeditedit: How do I get the name of the joining player to a variable?

Last edited by Zinc : 03-07-10 at 11:21 AM.
  Reply With Quote
03-07-10, 11:22 AM   #8
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
If the code you posted is the entire code, I'd just remove the xml-file completely. It doesn't seem to have any purpose, since you create the frame anyway in the lua-file.
__________________
Oh, the simulated horror!
  Reply With Quote
03-07-10, 11:27 AM   #9
Zinc
A Deviate Faerie Dragon
Join Date: Mar 2010
Posts: 13
Thanks, Ailae. Also, how do I get the name of the triggering player?
  Reply With Quote
03-07-10, 11:51 AM   #10
yj589794
A Rage Talon Dragon Guard
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 314
You can get the arguments passed with the event from wowprogramming or wowwiki

change your event handler routine to use the passed arguments like:

Code:
  local function autoResponse(event, _, sender, _, channelString, _, _, _, channelNumber, channelName, _, _)
    print("|CFF00FF00" .. sender .. " has entered the channel.|R");
  end
tidy up the string concatenation how you like, above is just an example
  Reply With Quote
03-07-10, 12:02 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Actually the first arg is a reference the the frame that belongs to the event handler. Then comes the event that occurred, etc.
__________________
"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
03-07-10, 12:50 PM   #12
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Code:
frame:SetScript("OnEvent", function(self, event, ...) if self[event] then self[event](self, ...) end end)
Just use that. Then if you register an event all output will go to the function frame:EVENT_NAME and all parameters will be given directly. So, if you want to register the event "ADDON_LOADED", for example, then this would be your code:

Code:
local frame = CreateFrame("frame")
frame:SetScript("OnEvent", function(self, event, ...) if self[event] then self[event](self, ...) end end)
frame:RegisterEvent("ADDON_LOADED")

function frame:ADDON_LOADED(addon)
    print(addon)
end
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
03-07-10, 04:09 PM   #13
Zinc
A Deviate Faerie Dragon
Join Date: Mar 2010
Posts: 13
Alright, I set it to display the second argument, but it is displaying CHAT_MSG_CHANNEL_JOIN anyway. The second argument is the joining player, according to the wowwiki article.

"CHAT_MSG_CHANNEL_JOIN"

Fired when someone joins a chat channel you are in

arg1
seems to be empty
arg2
Name of the player that joined
arg3
seems to be empty again
arg4
Number and name of the channel (e.g. "5. MyOwnChannel")
arg8
Channel number
arg9
channel name without number (this is sometimes in lowercase)
.lua
Code:
  local frame = CreateFrame("FRAME", "MParent");
  frame:RegisterEvent("CHAT_MSG_CHANNEL_JOIN");

  local function autoResponse(empty,joiner,emptyb,channel,number,nonmember)
    print("|CFF00FF00" .. joiner .. " has entered the channel.|R");
  end

  frame:SetScript("OnEvent",autoResponse);
Also, once I get the name instead of the.. whatever that is, can I just do this? Let's say I set the argument for the player's name to pname.
Code:
/script SendChatMessage("Welcome!" ,"WHISPER" ,nil ,pname);
Edit: Are all variables in lua global?

Last edited by Zinc : 03-07-10 at 04:21 PM.
  Reply With Quote
03-07-10, 05:20 PM   #14
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Seerah View Post
Actually the first arg is a reference the the frame that belongs to the event handler. Then comes the event that occurred, etc.
*ahem*

yj589794 said it's the 3rd return, but the player's name will actually be the 4th return.
__________________
"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

WoWInterface » Developer Discussions » General Authoring Discussion » Double Events

Thread Tools
Display Modes

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