Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How do I do this? — Gideros Forum

How do I do this?

Tom2012Tom2012 Guru
edited November 2012 in General questions
This should be easy but I'm wracking my brains trying to do it. :-)

I want to put a set of co-ordinates into a table...

coords = {}

table.insert(coords, x1,y1)
table.insert(coords, x2,y2)
table.insert(coords, x3,y3)

What code is it to add the x,y to the table please?

Thank you

Comments

  • hgvyas123hgvyas123 Guru
    edited November 2012
    try this method

    local coords = {
    {1,2},
    {11,55},
    {4,5},
    {134,233},
    }
    print(coords[1][2])


    or


    local coords = {
    {x=1,y=2},
    {x=11,y=22},
    {x=111,y=222},
    {x=1111,y=2222},
    }
    print(coords[2].x)
  • Tom2012Tom2012 Guru
    edited November 2012
    Thanks I'm trying to use:
    	for j=1,#layer.objects[i].polygon do
    			myShape:lineTo(layer.objects[i].polygon[j].x,layer.objects[i].polygon[j].y)
     
    			--local toInsert = layer.objects[i].polygon[j].x..','..layer.objects[i].polygon[j].y
     
    table.insert(coords, layer.objects[i].polygon[j].x..','..layer.objects[i].polygon[j].y);
    Then later..
    poly:set(unpack(coords))
  • Something like that anyway ;-)
  • OZAppsOZApps Guru
    edited November 2012
    @tom, have you tried
    coords = {}
     
    table.insert(coords, {x1,y1})
    table.insert(coords, {x2,y2})
    table.insert(coords, {x3,y3})
    You can find info like this and more in my book
    Learn Lua for iOS Game Development from Apress at http://www.apress.com/9781430246626
    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
  • Yep, but I need to be able to use:

    poly:set(unpack(coords))

    And for it to work like

    poly:set(23,11,66,77,88,55,33,44)

    I'm trying to feed coords dynamically into a table then read them into the poly:set box2d shape.

    Thanks
  • You can use this:

    table.insert(coords, x..","..y)

    But of course you get the error:

    main.lua:84: bad argument #1 to 'unpack' (table expected, got string)
  • @tom if you need to have them respond to unpack, then add them to the table, one at a time
     table.insert(coords,x)
     table.insert(coords,y)
     
     .
     .
    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
  • Ah, this worked:

    for loop

    table.insert(x)
    table.insert(y)

    end for loop

    then...

    poly:set(unpack(coords))
  • @Tom, the issue could also be that you might have misunderstood unpack. Unpack will display the elements of an array, so the moment you make it a hashtable/dictionary object/key-value pair, it will not work.

    So if you are expecting that unpack will unpack and return x=val, y=val, width=val, height=val, etc then you would head for massive disappointment.

    But if you insert several values either all at once as
     table.insert(coords,{1,2,3,4,5,6,7,8,99}
    or one by one as
     table.insert(coords,x)
     table.insert(coords,y)
    remember, if you add more than one value using the {}, you are not adding a value but a new array/table to that index.

    now if you use unpack(coords) it will work.
    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
  • @tom if you need to have them respond to unpack, then add them to the table, one at a time
     table.insert(coords,x)
     table.insert(coords,y)
     
     .
     .
    Ah you beat me to it. :-)

    Thanks OzApps.

    The stumbling block was I was thinking of pairs rather than individual numbers.
  • a little out of sync, glad that you got it resolved, if you need to understand unpack, you can read the above post.
    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
Sign In or Register to comment.