Lua Help Thread

  • jacksonmj
    12th Jun 2012 Developer 0 Permalink
    Also, tpt.mousex>188 and tpt.mousex<124 can't both be true at the same time (perhaps tpt.mousex<224 instead?)

    And it needs another "end" - one to finish the if statement and one to finish the function.
  • mniip
    12th Jun 2012 Developer 0 Permalink
    uuhh yeah, that too
  • shutterbug
    16th Jun 2012 Member 0 Permalink

    Hmm... It still says " unexpected symbol near ')' "

    Although everything that people have said helped, it does not work :/

  • mrsalit0s
    28th Jun 2012 Member 0 Permalink

    currently i'm working on an element and could use some help. i need something like this:

     

    if type==tpt.el.sprk(with ctype merc).id then

     

    unfortunately i don't know how to write it correctly :/

  • vertigo
    28th Jun 2012 Member 0 Permalink

    I need to know how to use tpt.el.----.actas

    Thanks in advance

  • baizuo
    30th Jun 2012 Member 0 Permalink

    Lua help needed: How to get the lower boundary of a free table? See, you can get the upper boundary by table.maxn(), but, which I can't understand why, there's no table.minnn()

    Example:

    mtx={}

    mtx[2]=1

    mtx[-7]=42

     

    table.maxn(mtx)   --output:2

    --how to get the -7?

  • boxmein
    30th Jun 2012 Former Staff 0 Permalink
    @baizuo (View Post)
    Arrays only go from 0 to max-length. In Python at least, array[-x] will do array[len(array)-x+1]
    It appears so as in Lua tables also do that.
    http://www.lua.org/pil/24.2.3.html

    Quoting the Lua documentation:
    The lua_gettop function returns the number of elements in the stack, which is also the index of the top element. Notice that a negative index -x is equivalent to the positive index gettop - x + 1.
  • baizuo
    30th Jun 2012 Member 0 Permalink

    @boxmein (View Post)

    Seems...not really.

  • mniip
    1st Jul 2012 Developer 0 Permalink
    There is no special data type for arrays in Lua
    An array is considered a table with only numeric keys set, and if they continuously go from 1 to n,
    where n is array length.
    #t, table.len(t) would only work on arrays, they would return some wrong data if used on non-arrays
    table.maxn just iterates over all numeric keys and shows the highest
    It could be rewritten as
    function table.maxn(t)
    local l=0
    for i in pairs(t) do
    if type(i)=="number" and i>l then
    l=i
    end
    end
    return l
    end

    Just write table.minn function as this and use for your needs
  • baizuo
    1st Jul 2012 Member 0 Permalink

    @mniip (View Post)

    Thanks for your reply

    I found few documents, they say that, the table in lua act like this:

    arr[k] is similar to normal array if k>0

    Any situation else, arr[k] is similar to hash table

     

    Did that means, m[{number}] is part of the array, but m[{Non-number}] is in the hash table part?

    For example mtx: (in sequence)(pseudo code)

    mtx={

     '1':xxx

     '2':xxx

     ...

     '9999':xxx

     '0'.getHash():xxxx

     '-2'.getHash():xxxx  (And they are ordered by the hash but not the 'key' itself)

    }

     

     

    So in my case, I can use the function below which you suggested instead:

    function table.minn(t)
    local min=0
    for i in pairs(t) do
     if type(i)=="number" and i<min then
      min=i
     end

    end
    return min
    end

     

     

    BTW, I quit the free table idea already, it's not a good structure indeed. Using offset variable instead.