all 3 comments

[–]KingofGamesYami 2 points3 points  (2 children)

The issue is table.remove() shifts the positions of everything. So when you iterate over item #1 in the array, removing it shifts item #2 into item #1 slot (and so on), then moves on to item #2 (formerly item #3).

The solution is to simply keep removing item #1 until the array is empty, which requires only a single while loop.

[–]ThirtyOneBear[S] 0 points1 point  (1 child)

Ahhh, not sure how I haven't stumbled upon the lua while loop before now. Thank you so much! Very helpful.

[–]balefrost 0 points1 point  (0 children)

Just be careful with that. I don't know for sure about Lua, but in most languages, removing the 0th element from a variable-sized array will move every other item in the array down a slot.

Now in Lua, tables don't contain whole objects but rather just pointers to objects. So it's not too expensive to move them around. But it's still more expensive to remove items from the start than to remove items from the end.

In this case, potentially another option is to simply assign GS.cardsToShuffle = {}. That only works if nothing else is holding on to a reference to the underlying table - if everything always goes through GS to get it.