Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
lua - why the second was the first? — Gideros Forum

lua - why the second was the first?

sslivkasslivka Member
edited April 2013 in General questions
source:

function f1(...)
for i,v in pairs(arg[1]) do
print(i, v)
end
end

f1({["sound1"] = "sound1.mp3",
["sound2"] = "sound2.mp3",
["sound3"] = "sound3.mp3",})

result:

main.lua is uploading.
Uploading finished.
sound2 sound2.mp3
sound1 sound1.mp3
sound3 sound3.mp3

why the second was the first?

Comments

  • Lua pairs function doesnt guarantee orders. http://www.lua.org/pil/7.3.html
    have fun with our games~
    http://www.nightspade.com
  • If order is important to you, then you can use a numeric array.

    However if you are insistant about using alphanumeric arrays and want them to be ordered as Sound1 ,Sound2 and Sound3, then the way for that would be
    function(...)
     
    -- temp  is an array that already has the files added into it and have the convention SoundX.mp3, where X is a number from 1 to 9999
     
     for i=1,3 do -- or you can use the #temp (for all items)
      print(i .. " = " .. temp["Sound" .. i])
     end
     
    end
    However I would simply use a numeric array as it is faster AND I can still access sounds[1], sounds[2] and sounds[3] rather than sounds["sound1"], sounds["sound2"] and sounds["sound3"]

    hope that helps you in some ways
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • No it is not important to me, just wondering why. Thank you for your answers!
Sign In or Register to comment.