WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Double Events (https://www.wowinterface.com/forums/showthread.php?t=31104)

Zinc 03-07-10 12:04 AM

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:

Quote:

Someone has entered the channel.
Someone has entered the channel.

Sekrin 03-07-10 03:09 AM

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).

Zinc 03-07-10 08:10 AM

Quote:

Originally Posted by Sekrin (Post 180841)
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.

Rilgamon 03-07-10 10:19 AM

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 ?

Zinc 03-07-10 10:57 AM

.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.

Ailae 03-07-10 11:14 AM

You're loading HelloWorld.lua twice, once in the .toc and once in the .xml. Ergo, you get double messages.

Zinc 03-07-10 11:16 AM

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?

Ailae 03-07-10 11:22 AM

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.

Zinc 03-07-10 11:27 AM

Thanks, Ailae. Also, how do I get the name of the triggering player?

yj589794 03-07-10 11:51 AM

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

Seerah 03-07-10 12:02 PM

Actually the first arg is a reference the the frame that belongs to the event handler. Then comes the event that occurred, etc.

nightcracker 03-07-10 12:50 PM

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


Zinc 03-07-10 04:09 PM

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.

Quote:

"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?

Seerah 03-07-10 05:20 PM

Quote:

Originally Posted by Seerah (Post 180891)
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.


All times are GMT -6. The time now is 10:44 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI