Basically, I have a group of characters in a table. Let’s call them shoppers.
- They buy something, and then at some point they disappear.
- Once they are offscreen I remove them from the table as they are not supposed to come back.
- However, I have a pause function. It works pretty simply, like this:
for i = 1, #shoppers do
shoppers[i]:toggle() -- pause or unpause depending on the game
end
- As you can see, it just iterates through the table, pausing each customer.
- The problem is, a customer could be removed from the table while this is happening. So suddenly 1, #shoppers is no longer reliable and it toggles a customer twice. Or skips one. Or crashes because inside the toggle code it’s referring to [i] which no longer means what it says it means.
Is there any sort of strategy people use for stuff like this? Ideally I would love to halt the table removal until after the pause completes, but I don’t see any useful way to tell if something like that is in progress. The best I can come up with is:
local maxshoppers = #shoppers
for i = 1, #shoppers do
if shoppers[i].paused == false then
shoppers[i]:toggle()
end
end
if #shoppers ~= maxshoppers then
-- do the same thing over again
end
…but all that solves is making sure everything pause/unpauses. It doesn’t solve the problem of it addressing the wrong objects.
Any ideas or approaches you’ve used would be awesome!
[import]uid: 41884 topic_id: 23356 reply_id: 323356[/import]
When it’s pressed, I do two things: