Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What would be the best way to handle elements in an array constantly needing to be removed?

I'm working on a GUI-based particle system and I've been introduced with a bit of an issue regarding the table handling the particles themselves.

My Issue

I have a table, particleList, that holds the data of each individual particle active every frame. I'm inserting data into this table using table.insert, meaning each new element shows up after the last item. The issue lies where the particles' lifespan ends, when I want the handler to remove the particle itself and stop handling it altogether (removing it from the table).

Most commonly, the particles' lifespan would run out in order from first created to last created, meaning it's almost always the first particle in the table that's removed. My first plan was to use table.remove to remove the particles from the table, but I was told by reputable sources that every element after the removed has to be copied and shifted backwards. As I'm going to be handling up to hundreds of particles at a time, I fear this will be too hard on the game to do this on a frame-by-frame basis.

My Hypothesis

My first idea to fix this was to set removed particle data to nil instead of using table.remove. The problem here is pretty obvious: there will be many holes left inside the table, especially over time.

My second idea was to have a separate function that runs at a different, much slower interval to clear the table of any nil values using a set of table.remove calls, possibly every few seconds. Though, I fear this is also too hard on the game as it's only condensing the remove calls into one place.

My third and current idea is to insert particles into the holes in the array rather than using table.insert. Though I'm not sure how painful it would be on the game itself to search through the table for the nil values every time a new particle is created. This seems like the best solution to me thus far.

My Question

Is there a definitive way to do this? My current plan is to use the third idea but if there's a better way to handle this or one of my ideas is simply the best, I'd appreciate this being made clear to me.

Thank you!

2 comments

0

You want to make ParticleEmitters in a GUI?

0

Essentially, though this isn't the question at hand

1 answer

✓ Accepted answer

I think that the best method would be to search for a nil value and when you get there to replace that element. For this I would use ipairs because it stops at a nil value and wont waste energy searching through the rest of the loop or make weird errors. Here is an example:

local exampleTable = {} 
local neededIndex = false
-- when you need to
for i,v in ipairs(exampleTable) do
	if not exampleTable[i + 1] then 
		neededIndex = i + 1
	end
end
if not neededIndex then -- in case the first element of the table was nil
	neededIndex = 1
end
-- then just insert the value you need into the table at index of neededIndex

I hope this is what you are looking for and that it helps. Have a great day scripting

4 comments

0

Wouldn't you have to ipairs() the table lol

0

lol I will fix that

0

Wouldn't "if not neededIndex then" be the same as "if false then"? Because numbers are truthy in Lua are truthy, and the "not" evaluates to "if false then".

0

On line two I set needed index to false. I only want the if statement on line nine to pass if a number was not assigned in the loop. That could only mean that the open spot is at the index of one.

This discussion is preserved as it was. New answers, comments, and votes are closed.