Function that removes a respective array from a toExecute table upon completion

init = function(input) 
  print("init took ",input)  
  --something to remove it from toExecute
end

toExecute = { 
	{init,"foobar"}
    {init,"barfoo"}
}

while true do wait(.3) -- this should only print foobar and barfoo each once
    for i,v in ipairs(toExecute) do
	print(i,v)
	v[1](v[2])
   end
end

I think I could use self, but I’m not sure how. I can manage this by passing an additional reference parameter, but that feels so backwards and just wrong imo. that I hope there’s a better way.

this will work better

local toExecute = {
	"foobar";
    "barfoo";
}

local function init(input, num)
	print(input)
	table.remove(toExecute, num)
end

while wait(0.3) do
    for i,v in pairs(toExecute) do
		init(v, i)
   end
end

The init function is a placeholder for something arbitrarily more complex. Though thank you for the attempt. I’d appreciate a PM as to why this was not obvious also.

Are you trying to remove the execution from the array after it executes? You could probably use the return for that. The execution will return nil, and any index in a table with a nil value is considered to not exist.

while true do wait(.3) -- this should only print foobar and barfoo each once
    for i,v in ipairs(toExecute) do
	    print(i,v)
	    v = v[1](v[2])
    end
end

Could you explain your problem in more detail please.

Can’t you just do

toExecute[i] = nil;

upon completion would place this at the end of the arbitrary init function. So you’d need to pass the i as a parameter into the function so it would know what it’s a part of. This is the way I know I could do, that just makes me feel I could do it better.

If you meant having that externally, I’m sure it would always execute at the start of the function. Possibly a weird custom event ended could extend this, but that’s also a wonky behavior imo especially with its complexity.