Trying to achieve two simple effect that is common in casual games.

While we are at it, what if a display object is added to a table. Does calling display.remove and setting nil to it enough or I have to remove it from that table as well?

I think we have to because that array has it’s reference and doesn’t allow reference count to be zero, right? [import]uid: 206803 topic_id: 35718 reply_id: 145481[/import]

If you do:

mytable[5] = nil  

after removing the object, then the reference count on the memory will drop by one and if it’s not in use, it gets freed. You don’t need to nil the whole table.

Just keep in mind that if you nil an entry that’s using a numeric index and there are entries higher than what your nilling (5 in my example) then the # operator will stop where you nil’ed and won’t count higher values. There is a table function that will return the true count.
[import]uid: 199310 topic_id: 35718 reply_id: 145591[/import]

[quote=“rob,post:42,topic:310515”]

If you do:

mytable[5] = nil

after removing the object, then the reference count on the memory will drop by one and if it’s not in use, it gets freed. You don’t need to nil the whole table. Just keep in mind that if you nil an entry that’s using a numeric index and there are entries higher than what your nilling (5 in my example) then the # operator will stop where you nil’ed and won’t count higher values. There is a table function that will return the true count.
[import]uid: 199310 topic_id: 35718 reply_id: 145591[/import] [/quote]

 

Rob would you mind expanding on this a little bit?  I am working on something where I nil something out from a table and am having a problem when trying to reinsert objects into the table.  What is the function you talk about that returns the true count?  Is there a way to sort of reset the index or something so that the higher values can be counted?

 

Sorry if I didn’t explain my questions well, i’m not too knowledgeable on the subject.   

The function is table.maxn()

 

In the example above if you nil out a middle entry in the array, say entry #3, you can just assign your data back to array[3] = something to repopulate that cell.   If your array contains simple data (not display objects, sounds, or other large things), you can simply overwrite the cells as necessary.

 

Maybe if you post some code inside of [lua] and [/lua] tags (without spaces) I might understand your needs better.

Hmmm, I think the best way would be maybe to tell you what i’m trying to do conceptually because that is probably where my problem lies.  

 

I am creating a card game that shuffles 52 cards and inserts them as display objects into a table.  Each hand some cards are dealt and as each one is dealt they are inserted into one of two groups.  At the end of the hand the cards are removed via:

 

    for i = playerGroup.numChildren, 1, -1 do          local child = playerGroup[i]         child.parent:remove(child)         child = nil     end      for i = dealerGroup.numChildren, 1, -1 do          local child = dealerGroup[i]         child.parent:remove(child)         child = nil     end  

 

Now, the problem falls when I get to the end of the deck and have to reshuffle the cards, and try to restart the same process as before.  

 

function reShuffle()     if counter \>= 10 then          setupCards()         backs()         counter = 0         playerGroup:toFront()         dealerGroup:toFront()     end  end   

 

So this reshuffles the cards and should basically start with a fresh deck.  The counter is reset and the next cards will be dealt from the first index of the table.  I believe the problem is happening when the cards are being dealt this time they are trying to be reinserted to the same display group as before (I just realized after typing all of that i’m not having a problem with the array but instead the display group… oops maybe you can still help anyways).  Then I get this error:

 

 

2/main.lua:197: bad argument #-2 to ‘insert’ (Proxy expected, got nil)

 

at the line where I am trying to reinsert the card display object into the display group via:

playerGroup:insert(cardTable[cardVal[counter]])  

 

Hope I explained this well enough… any ideas where I might be going wrong?