Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Reference a table several times in another table, and assign different id for each — Gideros Forum

Reference a table several times in another table, and assign different id for each

MellsMells Guru
edited January 2013 in General questions
Hi all,

I know, I couldn't write a good title.

I have an issue with arrays.
Here is some pseudo code :
local img1 = display:newImage(imgBase.."bg1_500.png", 0, 0)
	local img2 = display:newImage(imgBase.."bg2_500.png", 0, 0)
	local img3 = display:newImage(imgBase.."bg3_500.png", 0, 0)
	local img4 = display:newImage(imgBase.."bg2.png", 0, 0)
	local imgList = {img1, img2, img3, img4}
If I want to give an Id to each image based on its key, I do
local i
local container
for i = 1, #imgList do
     imgList[i].id = i
     container:addChild(imgList[i])
end
 
-- print imgList -> 1, 2, 3, 4
-- print #container -> 4
However if I have :
local imgList = {img1, img2, img3, ***img2***}
local i
for i = 1, #imgList do
     imgList[i].id = i
end
 
-- Problem :
-- print -> 1, ***4***, 3, 4
-- print #container -> ***3***
I understand what's happening there.

Question

What would be the way to have the same image (which is a table) appear several times in an array and each one have its own id number? (1, 2, 3, 4)
I'm sure there is some magic lua trick like a special character. What is the cheat code please? :)

thank you!
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • did you try creating a table of images as
    img = {"img1.png","img2.png","img3.png"}
    then you can reference these in the new table as
    imgList = {1, 2, 3, 2, 1}
    and then use them accordingly, you think that could help achieve what you are after or is it something totally else.
    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
  • MellsMells Guru
    edited January 2013
    @OZApps
    First, thank you for helping me.

    i am trying to stitch images so that when I give {img1, img2, img3, img4}, the Class understands how to position images. I need to store the key because I do all sort of remove/insert later and the only reliable way (to my knowledge) to retrieve the right image is to store its unique id from the start.

    But if I do :
    {img1, img2, img3, img2}
    the second time my loop sees img2, it will affect the same key to the first img2 because it doesn't see it as different instances (because it's not).

    I would like to avoid to create two different objects when I create my images
    local img2 = display:newImage(imgBase.."bg2_500.png", 0, 0)
    local img4 = display:newImage(imgBase.."bg2_500.png", 0, 0)

    I want img1.id = 1
    img2.id = 2
    img3.id = 3
    img2.id = 4
    and couldn't find a way to get it

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • OZAppsOZApps Guru
    edited January 2013
    Ah, you want to create an new instance based on the numbers, like a tile (is that correct ?) then...
    img = {"image1.png", "image2.png", "image3.png", "image4.png"}
    imgList = {1,2,3,4,3,2,3,2,1}
    and in the loop, you need to load the image so that you get..
    imgArry = {}
    for i = 1, #imgList do
      local _img = Bitmap.new(img[imgList[i]])
      stage:addChild(_img)
      imgArray[i] = _img
      _img.id = i
    end
    Now you can access the same either via id or by index.

    Likes: Mells

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • @OZApps
    yes that is it but I wanted to avoid creating the same image twice before sending it to my function so that I could do :
    local imgList = {img1, img2, img3, img2}
    I would like it to be hidden to the user so I think I'll handle it on the Class side.

    That's why I was searching a way to create a deep copy of a Bitmap object :
    if img2 appears twice in the array, create a new instance.
    I could do it if "img2:getTexture()" was available so that I could create a new instance from the texture.

    But I don't know how to create a copy of a Bitmap object without it.
    Any idea?

    Edit : available in gideroscodingeasy
    function Bitmap:getTexture()
    	return self.texture
    end
    great :)

    Any other and better way to create a deep copy of a bitmap object without getTexture/create new bitmap?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @Mells, since each new bitmap is still using the same texture and hence efficient on memory, why do you want to make a deep copy and then change the attributes? I do not see any harm in creating a duplicate image (a new instance) than trying to make a deep copy.

    Anyways, not sure on why you want to do it this way.
    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
  • MellsMells Guru
    edited January 2013
    @OZApps
    It was hard for me to explain the case because it's very specific, I needed to get the number of objects added to a container and other things.
    Deep copy was not the right term.
    I wanted to be able to do "img5 = img2" so that a new instance of that Bitmap object is created without Bitmap.new().
    I thought there would be a special character like img5=$img2 that would make it possible (I don't know, but in that case img5 wouldn't point to img2 but would have its own address).
    It was important for me to get an array of images already constructed somewhere else.

    That's sorted now, I create a different Bitmap in my class with Bitmap.new().

    But really, I thought there is a way to do img5 = img2 with different addresses in memory.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Deep Copy is the correct term when it clones an object not just point to it.

    The point that I do not understand is what difference does it make if you used Bitmap.new() vs a deep copy, specially since it would provide the same, unless you had subclassed the Bitmap with custom functions, etc.

    If you want to write personally, you could and try to explain what you are after.
    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.