Thread Tools Display Modes
06-03-13, 08:27 AM   #1
gibogubo
A Defias Bandit
Join Date: Jun 2013
Posts: 2
Problen using events

Hello!
I´ve been programing addons for a bit, but now that i´m trying to use events some problems appear...
This addon is supossed to send a chat mesage when the player leves up and receives a whisper whith the message "Hello!", but it does nothing...

Can anybody tell me where´s the error?
Code:
local creat = CreateFrame("FRAME", "TestingFrame");

function TestingFrame_OnLoad(self)
	self:RegisterEvent("PLAYER_LEVEL_UP");
	self:RegisterEvent("CHAT_MSG_SYSTEM");
end

function TestingFrame_OnEvent(self, event, ...)
	local arg1 = ...;
		if event ==  "PLAYER_LEVEL_UP" then
			local level = ...
			SendChatMessage("I´ve leveled up!", "SAY");
		elseif event == "CHAT_MSG_SYSTEM" then
			local message = arg1;
			if string.find(message, "Hello!", 1, true) ~= nil then
				SendChatMessage("Somebody has whispered me!", "SAY");
			end
		end
end
  Reply With Quote
06-03-13, 09:16 AM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
This is how I would write this..
Lua Code:
  1. local f = CreateFrame('frame')
  2. f:RegisterEvent('PLAYER_LEVEL_UP')
  3. f:RegisterEvent('CHAT_MSG_WHISPER')
  4.  
  5. f:SetScript('OnEvent', function(self, event, ...)
  6.     if event == 'PLAYER_LEVEL_UP' then
  7.         SendChatMessage("I've leveled up!")
  8.     else
  9.         local message = ...
  10.         if message:find('Hello!') then
  11.             SendChatMessage('Somebody has whispered me!')
  12.         end
  13.     end
  14. end)
I think you meant CHAT_MSG_WHISPER instead of CHAT_MSG_SYSTEM, but if not feel free to change it.

You also don't need string.find if you're matching literally the message "Hello!", you could just compare the string like if message == "Hello!". String.find is for when you want to search through the entire string for any instance of the term.

It's also case sensitive, so you could call string.lower on it and compare it to 'hello!' if you want to match any case.

Last edited by semlar : 06-03-13 at 09:20 AM.
  Reply With Quote
06-03-13, 10:00 AM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by gibogubo View Post
Hello!
I´ve been programing addons for a bit, but now that i´m trying to use events some problems appear...
This addon is supossed to send a chat mesage when the player leves up and receives a whisper whith the message "Hello!", but it does nothing...

Can anybody tell me where´s the error?
Code:
local creat = CreateFrame("FRAME", "TestingFrame");

function TestingFrame_OnLoad(self)
	self:RegisterEvent("PLAYER_LEVEL_UP");
	self:RegisterEvent("CHAT_MSG_SYSTEM");
end

function TestingFrame_OnEvent(self, event, ...)
	local arg1 = ...;
		if event ==  "PLAYER_LEVEL_UP" then
			local level = ...
			SendChatMessage("I´ve leveled up!", "SAY");
		elseif event == "CHAT_MSG_SYSTEM" then
			local message = arg1;
			if string.find(message, "Hello!", 1, true) ~= nil then
				SendChatMessage("Somebody has whispered me!", "SAY");
			end
		end
end
First of all, without registering your functions as the frame's script handlers, those functions will never run. For example, you'll need to add this line to register your OnEvent handler.
Code:
creat:SetScript("OnEvent",TestingFrame_OnEvent);

Note it's impossible to have Lua create a frame and then register an OnLoad for it to run immediately as the OnLoad handler runs as a part of the CreateFrame() call. Registering the handler function does nothing. Instead, you should move the code contained in the function into the main chunk.
Code:
local creat = CreateFrame("FRAME", "TestingFrame");
creat:RegisterEvent("PLAYER_LEVEL_UP");
creat:RegisterEvent("CHAT_MSG_SYSTEM");


With these modifications and the fact you don't need to create a global function to use as a script handler, your code should look something like semlar's post above.
__________________
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)

Last edited by SDPhantom : 06-03-13 at 10:09 AM.
  Reply With Quote
06-03-13, 10:38 AM   #4
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
If a frame registers more then 3 events, I personally like to set up a table approach for events. I realise this may be overkill, but it sure helps if your AddOn is properly divided into sections. As a bonus, you no longer have to type "local level, hp, mp = ..." and you can basically skip the event type checking.
lua Code:
  1. local frame = CreateFrame('Frame')
  2. frame:SetScript('OnEvent', function(self, event, ...)
  3.     assert(self[event], 'No event handler for event' .. event)
  4.     self[event](self, ...)
  5. end)
  6.  
  7. function frame:PLAYER_LOGIN()
  8.     -- player has logged in
  9.     self:RegisterEvent('PLAYER_LEVEL_UP')
  10. end
  11.  
  12. local levelUpMsg = 'Level: %d, extra health:%d, extra mana:%d'
  13. function addon:PLAYER_LEVEL_UP(level, hp, mp)
  14.     print(levelUpMsg:format(level, hp, mp))
  15. end
  16.  
  17. --[[
  18.     IsLoggedIn()
  19.     - Returns nil before the PLAYER_LOGIN event has fired, 1 afterwards.
  20.     - Player data is available once PLAYER_LOGIN fires, so either wait for it,
  21.       or run it if player is already logged in ( /reloadui )
  22. ]]
  23. if IsLoggedIn() then
  24.     addon:PLAYER_LOGIN()
  25. else
  26.     addon:RegisterEvent('PLAYER_LOGIN')
  27. end
Thanks to the awesome tekkub for coding in this fashion, I haven't written eventlisteners any otherwise since I discovered this trick

Last edited by ravagernl : 06-03-13 at 10:42 AM.
  Reply With Quote
06-03-13, 01:44 PM   #5
gibogubo
A Defias Bandit
Join Date: Jun 2013
Posts: 2
Thanks all of you guys, now it´s working as intended
  Reply With Quote
06-03-13, 04:17 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Also, another problem in your original code was that you were registering for CHAT_MSG_SYSTEM -- which fires when a yellow system message like "Soandso has come online" -- instead of CHAT_MSG_WHISPER -- which fires when another player sends you a whisper message.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Problen using 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