1. #1

    [LUA] If string not working

    Hello! You might remember me from such classics as 'Asked for help yesterday' and 'guy rips hair out because hes stupid', I've been working on trying to get a IF string work to shorten the name of targets if its too long, but I cannot get it to work and I just can't figure out what I'm doing wrong. I looked online and as far as I can tell, my code looks correct.

    PHP Code:
    function()
        
        -- 
    Set Level Variables
        local Level 
    UnitLevel("target")
        
    local Level "[" .. Level .. "]"
        
        
    -- Set Name Variables
        local Name 
    GetUnitName("target")
        
    local NameL string.len(Name)
        
        -- If 
    Name is longer then 12
        
    -- cut it off
        Number 
    "12"
        
    if NameL >= Number then
            local Name 
    Name:sub(1,12) .. "..."
        
    end
        
        
    -- Display
        local Display 
    Level .. Name
        
    return Display
        
    end 
    All this returns is a ? by the way, thanks in advance, again. ._.
    Boom!

  2. #2
    Deleted
    not sure about your if statement right now. But why don't you just return "Name:sub(1,12)". This way you cut it off after the 12 character and as long as the name is shorter the full name is shown or am I missing something?

  3. #3
    The code/UI I'm working on is more so me learning LUA and the WOW API than it is just trying to do everything properly. So even if I end up not using the IF statement here, I'll be using it in other places, so I might as well learn why it's not working now.
    Boom!

  4. #4
    I didn't work with the LUA and the WOW API but in your IF statement, you are comparing a string ("12") with a number ("NameL"), and it will not work because it's 2 different type (in other programmation language at least) so if you want to compare string lentgh with a number, NameL is ok (it's a number return by len()), your variable Number should be 12 and not "12". If you want to compare between strings themselves, you have to compare variable with "" (like "Hello" != "allo").

    Hope it helps to understand the IF statement.

  5. #5
    Deleted
    Quote Originally Posted by LordKörtan View Post
    snip
    Your code has some errors in it, I believe you can change it to this:

    Code:
    function()
        
        -- Set Level Variables
        local Level = "[" .. UnitLevel("target") .. "]"
        
        -- Set Name Variables
        local Name = GetUnitName("target")
        local NameL = string.len(Name)
        
        -- If Name is longer then 12
        -- cut it off
        local Number = 12
        if NameL > Number then
            Name = Name:sub(1,12) .. "..."
        end
        
        -- Display
        local Display = Level .. Name
        return Display
        
    end
    Number should be an actual integer and not a string because you cant compare a number to a string value if you want the length compared. Also shortened your level variable to a single one and you only need to add local to the first mention of a variable and not every time you use it. It just needs to be initiated as local.

    For the if statement you can also remove the = sign because it is not needed as if the name is 12 you dont need to shorten it to 12.
    Last edited by mmoc587b332d2b; 2015-03-06 at 12:41 PM.

  6. #6
    Blademaster Odylle's Avatar
    7+ Year Old Account
    Join Date
    Mar 2015
    Location
    Netherlands
    Posts
    33
    Lots of code, but I think lots of it is very unnecessary
    I think the following just works as well.
    If you just always return the first 12 characters of the name, you do exactly what you want? I dont believe you need a IF statement there. Just always return the first 12.
    Code:
    function()
        return string.format("[%s] %s", UnitLevel("target"), GetUnitName("target"):sub(1,12))
    end

  7. #7
    Appriciate all the help! Thanks guys.
    Boom!

Posting Permissions

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