1. #1

    [WA/Lua] Trying to understand better.

    Hello. As the title suggests, I'm trying to get a better understanding of Lua and how it works. This evening, I had been messing around with a custom Lua function in WeakAuras that would 1) check the specified unit faction (in this case, the player) and then 2) check if said unit was also flagged for PvP. I've been trying to get more comfortable writing Lua on my own without looking up guides and just referring to API docs. Here's the code I created, which was intended to be debugging code while I worked on the actual aura:

    Code:
    function()
    
          local unit = UnitFactionGroup("player") --[[ Save myself the trouble of rewriting UnitFactionGroup repeatedly and instead just write "unit" ]]
          if (unit ==  'Alliance') then return "This unit is Alliance"
                 if (UnitIsPVP("player") == 1) then return " and is flagged for PvP."
                 else return "and is not flagged for PvP." end
          else return "An error occured." end
    
    end
    However, WeakAuras threw back the following error: "string "return function()...":5: 'end' expected (to close 'if' at line 4) near 'if'" Does anyone know why it's asking for an end statement for the first 'if' statement despite there already being one? Where did I make an error? I really want to learn how to code in Lua properly, and although I'm sure I'll feel like a complete idiot later, I figure it doesn't hurt to ask for some advice now. I've only really been pushing to learn the language for a few weeks now, but I've been trying to gradually get more advanced. Though, I will admit, "advanced" for me is still pretty freaking basic.

    Thanks
    Last edited by Darsyek; 2016-07-18 at 03:59 AM.

  2. #2
    Return ends a function. Lua is looking for either else or end after return or whatever object you're returning, but it ran into an if statement after that Alliance string.

    Correct examples:

    return end
    return "string" end
    if var then return var else return false end

    A function will only ever use one return line, however you can return a table, a concatenated string, or even multiple values separated by commas.

    Also, a tip for formatting to make it easier to keep track of where ends should be, put anything that needs an end on its own line and indent anything in that part the same. Ignoring the return issue, here's your formatted code:

    Code:
    function()
        local unit = UnitFactionGroup("player") --[[ Save myself the trouble of rewriting UnitFactionGroup repeatedly and instead just write "unit" ]]
        if (unit == 'Alliance') then
            return "This unit is Alliance"
            if (UnitIsPVP("player")) then
                return " and is flagged for PvP."
            else
                return " and is not flagged for PvP."
            end
        else
            return "An error occured."
        end
    end
    Originally Posted by Zarhym (Blue Tracker)
    this thread is a waste of internet

  3. #3
    Quote Originally Posted by Kanegasi View Post
    Return ends a function. A function will only ever use one return line, however you can return a table, a concatenated string, or even multiple values separated by commas.
    Alright, that makes a lot of sense. And yeah, I apologize for the formatting of my code. Normally I would space it out like your example, but I tend to compact it that way for use in WeakAuras because the window is so short and it lets me see more of what's going on.

    So, would a correct example be:

    Code:
    function()
         local unit = UnitFactionGroup("player")
         local isAlly = "This unit is Alliance " --[[Again, save myself from writing this multiple times]]
         if (unit == 'Alliance') then
              if (UnitIsPVP("player") then
                   return isAlly.."and is flagged for PvP."
              else
                   return isAlly.."and is not flagged for PvP
              end
         else
              return "An error occured."
         end
    end

  4. #4
    Quote Originally Posted by Darsyek View Post
    So, would a correct example be:

    Yes, that is a working example, kind of

    There were still a couple of syntax errors in there. Typo when you posted?
    Try testing it in the WeakAuras debugger first when you edit it, the indenting is done automatically at least

    Code:
    function()
    	local unit = UnitFactionGroup("player")
    	-- Again, save myself from writing this multiple times
    	local isAlly = "This unit is Alliance "
    	if unit == "Alliance" then
    		if UnitIsPVP("player") then
    			return isAlly.."and is flagged for PvP."
    		else
    			return isAlly.."and is not flagged for PvP."
    		end
    	else
    		return "An error occured."
    	end
    end
    Last edited by Ketho; 2016-07-18 at 10:56 AM.

Posting Permissions

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