Thread Tools Display Modes
05-10-12, 02:33 AM   #1
Doronamo
A Defias Bandit
Join Date: May 2012
Posts: 2
Newbie @ Lua, Simple Script Won't Work?

First off I'd like to say that I am about as noob as you can get for LUA, but I'm trying to learn

For my first addon, what I'm basically wanting to do is:

I send a message in my custom channel (najincom) containing "checkcurrency" and everyone in that channel that has this addon will respond with a chat message in that same channel.

So here's what I've got. (the whole TRP2_GetCountOfAll() is a function in TRP2)



Code:
local index = GetChannelName("najincom");
local najinsniff = CreateFrame("Frame");


function najinsniff_OnEvent()
     if (event=="CHAT_MSG_CHANNEL") and (arg1=="checkcurrency") and (arg9=="najincom") then 
          SendChatMessage("<Najin> I currently have " .. TRP2_GetCountOfAll("ITE050715543766345") .. " currency." [, "CHANNEL" [, nil [, index]]]);
     end
end

najinsniff:RegisterEvent("CHAT_MSG_CHANNEL");

Nothing seems to work, what am I missing here? It might also be important to note that my addon is called NajinCurrency, and the only two files in there are NajinCurrency.lua and NajinCurrency.toc. I'm writing all my code in .lua, obviously.
  Reply With Quote
05-10-12, 03:13 AM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
You registered the event, but you need to specifiy the action through SetScript.

Code:
najinsniff:SetScript("OnEvent", najinsniff_OnEvent)
For your first addon, it's also best to do simple things first like changing the colour of a frame or doing a test print() or something, just to make sure you're not forgetting something simple.
  Reply With Quote
05-10-12, 12:56 PM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
After following Haleth's instruction, you need to have your event handler function accept the arguments passed to it. This is the only way to get these values now, it was made available for use in BC and the globals were discontinued in LK. The function signature is as follows, function(self,event,...) where ... is the list of args from the event. You can use vararg here if you want or just supply local variable names to receive the values. Since your function only runs from one event, I'd suggest the later.

On another note, if you don't need to expose your function to the global environment, it's best to make it local. An example of both of these suggestions is to change the function declaration to the following.
Code:
local function najinsniff_OnEvent(self,event,arg1,_,_,_,_,_,_,_,arg9)
Note _ is a valid variable name, most people use it to skip values in a list by tossing it in to discard values they don't want.



There's also an error on the following line:
Code:
SendChatMessage("<Najin> I currently have " .. TRP2_GetCountOfAll("ITE050715543766345") .. " currency." [, "CHANNEL" [, nil [, index]]]);
The brackets as used here is not part of the Lua syntax. This is referred to as BNF notation to show which arguments are optional. In this case, every argument inside each group of brackets [ ] is optional The nesting of each set means one optional argument is dependent on another being supplied. Inserting this line into code will cause the Lua parser to throw an error.



With all of these concerns addressed, this is what I would suggest your code would look like.
Lua Code:
  1. local najinsniff = CreateFrame("Frame");
  2.  
  3. local function OnEvent(self,event,msg,_,_,_,_,_,_,num,name)
  4.     if event=="CHAT_MSG_CHANNEL" and msg=="checkcurrency" and name=="najincom" then
  5.         SendChatMessage("<Najin> I currently have " .. TRP2_GetCountOfAll("ITE050715543766345") .. " currency.","CHANNEL",nil,num);
  6.     end
  7. end
  8.  
  9. najinsniff:RegisterEvent("CHAT_MSG_CHANNEL");
  10. najinsniff:SetScript("OnEvent",OnEvent);
This also has been optimized in which the handler uses the channel number provided by the event to send its response. See http://www.wowpedia.org/Events/Commu...AT_MSG_CHANNEL for the list of arguments passed to the handler.
__________________
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
05-10-12, 03:46 PM   #4
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Oops, my bad for not noticing the other problems. I just looked at the most obvious one.

Also, perhaps worth noting is that if you use a function only once, you can declare it as an anonymous function.

Code:
local najinsniff = CreateFrame("Frame");
 
najinsniff:SetScript("OnEvent", function(self,event,msg,_,_,_,_,_,_,num,name)
    if event=="CHAT_MSG_CHANNEL" and msg=="checkcurrency" and name=="najincom" then 
        SendChatMessage("<Najin> I currently have " .. TRP2_GetCountOfAll("ITE050715543766345") .. " currency.","CHANNEL",nil,num);
    end
end)
 
najinsniff:RegisterEvent("CHAT_MSG_CHANNEL");
  Reply With Quote
05-10-12, 07:19 PM   #5
Doronamo
A Defias Bandit
Join Date: May 2012
Posts: 2
I cannot thank you guys enough. I will definitely learn from this, from my mistakes. I had no clue what the _ meant before, so thank you so much for clarifying that. Again, thanks so much.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Newbie @ Lua, Simple Script Won't Work?


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