Correctly Removing Object without Removing Array Index

Hi All,

I haven’t used Lua before now and have a quick question with regards to arrays of objects.

I have some arrays which look like this:
g.group[i][1] = Text
g.group[i][2] = Text
g.group[i][3] = Animation
g.group[i][4] = Image
g.group[i][5] = Text

The animation is essentially a loading animation that’s displayed while information is still being downloaded, however, the user can still click around while the info is being downloaded.

The problem is when I want the animation to be unloaded if I use display.remove or removeSelf it then readjusts the array making g.group[i][4] = g.group[i][3] (and 5 equal to 4), which then breaks my references to those elements in other code.

Is there are correct way to unload the animation from memory without removing the array entry entirely. I’ve tried setting it to nil but that errors. I can set it to a static image (which is what I want to do anyway), but I don’t think it’s then correctly unloading the animation. At the moment I’m just setting it to isVisible = false, loading an image in it’s place and carrying on. That works fine, but I’m assuming that is going to cause a pretty major memory leak over time.

Any help would be appreciated.

One other quick question too, I’ve found it easier to load specific things whether the app is loaded on iPhone or iPhone 4 as I don’t like the scaling features, however, this means that code below fails as I cannot declare the variables as local within an if statement, whats the best way to handle this?:
if display.contentWidth == 320 then
local imgArrow = display.newImage(“rightarrow_iphone.png”, true)
else
local imgArrow = display.newImage(“rightarrow.png”, true)
end
Cheers,

  • Will [import]uid: 61273 topic_id: 10641 reply_id: 310641[/import]

So… Why wouldn’t this work?

local imgArrow

if display.contentWidth == 320 then
imgArrow = display.newImage(“rightarrow_iphone.png”, true)
else
imgArrow = display.newImage(“rightarrow.png”, true)
end [import]uid: 50410 topic_id: 10641 reply_id: 38665[/import]

Ahh, thanks for that, I forgot you can declare variables without type in Lua! [import]uid: 61273 topic_id: 10641 reply_id: 38671[/import]

Hi Will,

What you can do is create an additional reference to the objects you’re adding to array at the same time as you add them. This reference is also attached to the array.

local groupa = display.newGroup();  
local Texta = display.newText("a", 0, 0, native.systemFont, 32);  
local Anim = display.newText("Anim", 0, 0, native.systemFont, 32);  
local Textb = display.newText("b", 0, 0, native.systemFont, 32);  
local Textc = display.newText("c", 0, 0, native.systemFont, 32);  
  
groupa:insert(Texta); -- add to array  
groupa.Texta = Texta; -- create extra reference  
  
groupa:insert(Anim);  
groupa.Anim = Anim;  
  
groupa:insert(Textb);  
groupa.Textc = Textc;  
  
groupa:insert(Textc);  
groupa.Textb = Textb;  
  
-- prints:  
-- [3].text=b  
-- Textb.text=b  
print("[3].text=" .. groupa[3].text);  
print("Textb.text=" .. groupa.Textb.text);  
  
groupa:remove(2);  
  
-- prints:  
-- [3].text=c  
-- Textb.text=b (so you can still get to Textb even though array index has moved)  
print("[3].text=" .. groupa[3].text);  
print("Textb.text=" .. groupa.Textb.text);  

I’m also new to lua but I’ve seen this used elsewhere successfully by grown-ups! :slight_smile:

[import]uid: 39538 topic_id: 10641 reply_id: 38697[/import]

Thanks for that Mark…not a bad way of doing things, although not sure of the extra variables. Ended up just creating placeholders e.g.
g.group[i][2].remove
g.group[i]:insert(2, “”)

So as long as as soon as I removed the element I created the placeholder until I was ready to insert the element again it worked beautifully without costing extra memory.

Cheers mate,

  • Will [import]uid: 61273 topic_id: 10641 reply_id: 38699[/import]

@willk4,
you know that you can reference tables like dictionaries with keys?

In simple language,

g.group[i][“text1”] instead of 1/2/3/4
and so on, so you can call them whatever you want, and replacing them is also easy

g.group[i][“text1”]=“New value”

however if the g.group is a displayGroup rather than an array as you have indicated in your first post, then this might not work as it for you.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10641 reply_id: 38710[/import]