1. #1

    LUA help - Tracking /rolls

    Hello everyone!

    I'm trying to become familiar with LUA and Addon Development and was wondering, how do I pull the results if someone /rolls?

    What I know is that when someone /rolls they're calling the RandomRoll() function. My current theory is that I'd have to pull the resulting string out of chat and format it in the way I'd like. I just can't seem to figure out how to pull strings from Chat.

    I know there are addons that track rolls already but I would like to do it for my own educational purposes.

  2. #2
    You have to use the event CHAT_MSG_SYSTEM.

    local f = CreateFrame("frame")
    f:SetScript("OnEvent", function(self, event, ...)
      if event == "CHAT_MSG_SYSTEM" then
        local message = ...
        local author, rollResult, rollMin, rollMax = string.match(message, "(.+) rolls (%d+) %((%d+)-(%d+)%)")
        if author then
            Do stuff here
    Last edited by aggixx; 2013-09-27 at 12:35 AM. Reason: Fixed error in matching pattern

    Druid / Demon Hunter SimulationCraft Maintainer

  3. #3
    Ok, couple of questions...

    1) what do the three dots mean when you initialize message?
    2) when I try to put rollResult in a message it comes up as being null after I do a /roll, why is that?

  4. #4
    I don't know the technical term but the when you define the function and you put "..." at the end, it essentially means "put any extra parameters passed into this function into ...", this is very useful for a function that handles events because very often you will be dealing with multiple events that return different number of parameters. So when CHAT_MSG_SYSTEM triggers, ... contains all the variables the event lists on the wowprogramming page, in order.

    In this case we're only interested in the first value "message" because the author of the message can be parsed from the message and all the other values we don't care about.
    local message = ...
    This line basically says "set message equal to the first value in ...", if we cared about the rest of the values we could instead do
    local message, sender, language, channelString = ...
    and then we would have those values in variables as well.

    In this case since we're only using that function defined in SetScript() to handle one event, we could've easily just defined the parameters as
    function(self, event, message)
      -- stuff
    but it's good practice to use ... there because if you later decide you need to register another event to the frame, you can handle the arguments of that event as well without having to modify your code. That's also the same reason I added the 'if event == "CHAT_MSG_SYSTEM" then end' structure even though with only one event it's completely superfluous.

    I don't see why rollResult would equal nil, are you sure you got the variable's name correct? I use that exact matching pattern in one of my addons and it's always worked for me.
    Last edited by aggixx; 2013-09-27 at 12:35 AM.

    Druid / Demon Hunter SimulationCraft Maintainer

  5. #5
    I copy/pasted your code into a test addon to see what I can do with it. So I'd imagine it's correct.

  6. #6
    Did you copy it before I added then "if author then end" part? Because I don't see what else would cause the issue; if author isn't nil that must mean the pattern matched the string so there should be non-nil values for all 4 variables.

    Druid / Demon Hunter SimulationCraft Maintainer

  7. #7
    nope I copied the author stuff too.

    Edit: I see what's going on, I'm trying to call message() which of course has the same name as the variable message... though now the window comes up blank.
    Last edited by Baelic; 2013-09-27 at 12:35 AM.

  8. #8
    Woops! Copied it from the wrong part of the code. Try this instead:
    local author, rollResult, rollMin, rollMax = string.match(message, "(.+) rolls (%d+) %((%d+)-(%d+)%)");

    Druid / Demon Hunter SimulationCraft Maintainer

  9. #9
    That worked, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts