Thread Tools Display Modes
09-19-12, 12:22 PM   #1
epicskillz
A Murloc Raider
Join Date: Sep 2012
Posts: 4
Slashcommand handler acting weird

Code:
function SlashPMS(msg, editbox)
  print("Entered /command handler " .. msg)
  PMSEncode(msg)
end

SLASH_PMS1 = "/pms";
SlashCmdList["PMS"] = SlashPMS;



--[[local function PMSEncode(msg)
  endResult = ""
  if msg == "" then
    print "Blank string."
  else
    print(string.len(msg))
    for i = 1, string.len(msg)-1, 1
      print("i")
--      encode = endResult .. PMSLetterEncoded(string.sub(msg,i,i+1) .. " "
    end
  end
  
  SendChatMessage(endResult, "SAY", "", "");
end 

local function PMSLetterEncoded(s)
  if s:len() > 1 then 
    return "0"
  else 
    return "1" -- here be the magic for output
  end
end

local function echo(str)
  print("|cfffef00fPMS |cff82e2eb" .. (str or ""))
end
--]]
I'm trying to make an addon that can "encode" your /say things. One day I might even implement a decoder, but that is a long way off.

First of all, as you see now, EVERYTHING except the slashhandler is commented out. Then it works.

If i do as little as uncomment the 3 functions, i just get /pms not recognized.

(Yes, PMS means Panzer's Messaging System. I also thought it was hilarious..)

Any help please?

My addon needs no GUI, my plan was to write /pms <any text> , and it would come out in /say as encoded text. Will that need any events(handlers)? I have somewhat experience with programming, but only been using lua for 40 mins, and never seen event-driven programming, so bare with me.
  Reply With Quote
09-19-12, 12:47 PM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
PMSEncode is not defined when you define SlashPMS. Either add a PMSEncode variable at the top, or move your PMSEncode function itself to the top.

The same goes for your other functions. When the interpreter loads a function, all of the variables used have to be defined.

Also, your endResult variable is a global. Might want to fix that.
  Reply With Quote
09-19-12, 12:53 PM   #3
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
Basically, SlashPMS needs to know where to "find" PMSEncode, and since you have PMSEncode's local being declared after SlashPMS, it can't find it during the initial parsing of the file.

Your options are basically either:

a) define SlashPMS after PMSEncode instead of before,

b) continue to define it after but add a line like "local PMSEncode" before SlashPMS so that SlashPMS knows a local called PMSEncode exists (the function itself doesn't need to be there until it's actually called), or

c) put the funtions into a table or similar (e.g. MyAddon.PMSEncode, MyAddon.PMSLetterEncoded) which is always defined at the top of your file, rendering the function order irrelevant since those table key lookups won't need to be resolved until runtime.
  Reply With Quote
09-19-12, 02:31 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
The above posts only answer for a solution, but don't explain why Lua is acting the way it does. If you want to use any upvalue (function or variable), they need to be defined as a local before the parser comes to the function and compiles it. If this is not done, the parser assumes the function is trying to reference a global variable instead and any upvalue defined afterward will not make it into its list.

Globals ignore this "define before use" rule because by default, variables encountered are seen as globals unless they have already been explicitly defined as a local in the current scope or any scope execution is nested in.
__________________
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 : 09-19-12 at 02:38 PM.
  Reply With Quote
09-19-12, 03:14 PM   #5
epicskillz
A Murloc Raider
Join Date: Sep 2012
Posts: 4
Thanks for fast replies!

I changed the order of the other 3 functions now, and I can see how and why my earlier setup caused problems. But still, there are some weird problems.

When I now write /pms, it doesn't even recognize that that is a valid function. It just says "Type '/help' for a listing of a few commands" meaning it doesnt recognize it at all anymore. With the functions commented out as in the example it recognises the addon command /pms. I changed Nothing other than the order they are presented in. (+ made endResult a local var)

I feel sorry for taking your time with silly problems, but one day I might be able to contribute back, if i learn the basics.
  Reply With Quote
09-19-12, 03:19 PM   #6
Tim
A Rage Talon Dragon Guard
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 309
You have both of your functions commented out.

--[[local function PMSEncode(msg)

along with --]] at the end of file.
__________________
AddOns: Tim @ WoWInterface
Battle Tag: Mysterio#11164
Current PC Setup: PCPartPicker List
  Reply With Quote
09-19-12, 03:56 PM   #7
epicskillz
A Murloc Raider
Join Date: Sep 2012
Posts: 4
Sorry, i was a bit eager, still not used to doublecheck my post for clarity.

I meant after doing the above suggestions AND uncommenting everything, /pms is not recognized. While all other functions are commented out, it works.
  Reply With Quote
09-19-12, 04:15 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Make sure you've got Lua errors enabled in your Interface Options. Install BugGrabber/BugSack. You've got an error occurring in your code and the parser isn't even making it to your slash command section.
__________________
"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
09-19-12, 04:27 PM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
I don't know about everyone else, but I'm able to get the default UI to display parser errors by doing a UI reload after an initial login.
__________________
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
09-19-12, 04:27 PM   #10
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Please post the full code again if you still have problems after following advice. And as Seerah said, make sure you have lua errors enabled.
  Reply With Quote
09-20-12, 04:39 AM   #11
epicskillz
A Murloc Raider
Join Date: Sep 2012
Posts: 4
Thanks for all help. After I enabled lua errors, things got a bit easier (yes, I'm new to this).

First off, i had forgotten (aka not known) you need for XX do. That do statement was new to me. Secondly, I had forgotten a closing parenthesis in
endResult = endResult .. PMSLetterEncoder(string.sub(msg,i,i+1)) .. " "
and lastly, I had written the concatenate strings statement wrong.

Now I've gotten over the first hurdles, and it seems to be working now. Nothing better than the learner discovering their own errors, eh?

Will bump for problems later on if they are relevant to thread subject. Huge thanks all!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Slashcommand handler acting weird


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