Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Array help — Gideros Forum

Array help

CreekmonkeyCreekmonkey Member
edited February 2017 in General questions
I'm new to Gideros and lua. I want to create an array of numbers (1 to 20). Then each time a button is clicked get a random number from that array, assign it to a variable then remove it from the array. I did this years ago in flash and as2. Any pointers?

Comments

  • may be like this?
    local a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
     
    math.randomseed(os.time())
    local random_number = math.random(#a)
     
    print("random number", random_number,"\n")
     
    local b = {}
     
    for i=1, #a do
    	if random_number ~= a[i] then
    		b[i] = a[i]
    	end
    end
     
    a = b
     
    for _,v in pairs(a) do
    	print(v)
    end

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • tab={1,2,5,8,10}
    function click()
         local val=table.remove(tab, math.random(#tab))
         return val
    end

    Likes: hgy29, muro, antix

    +1 -1 (+3 / -0 )Share on Facebook
  • Thanks for the replies. I am just getting started with Gideros so all help appreciated.
  • thanks keszegh, your example works great for my needs, except that once the table is empty then the script seems to get stuck and I get an error. Im sure there is a way to check that the table is not empty before executing the function but being new to lua I cant get a handle on it.
  • antixantix Member
    Accepted Answer
    if #table > 0 then
      -- use table in here
    end
Sign In or Register to comment.