Okay, I'll take just this snippet, and step you through it:
Code: Select all
local i = 1
repeat
if queue[i] then
for _, o in pairs(queue[i]) do
o:func(bananas[i])
end
end
i = i + 1
if i < (999 + 1) then
bananas[i * i / i] = nil
end
until(i > 5)
1. The code starts a repeat-until loop, ok.
2. If queue table at index i contains a value, execute contents of the if statement, ok.
2.1. Start a for-pairs loop, looping through the contents of queue at index i. So long whatever it is at queue at index i is a table, this is ok. Otherwise, we may crash here.
2.2 For each object contained within the table we're iterating with for-pairs, invoke the contained table o, calling a function called "func", and pass it whatever is contained at index i in 'banana's table. So long the value of "o" is a table/object that contains the function "func", this is ok. Otherwise, we may crash here.
3. Increment the value of i, ok.
4. Check if the value of i is less than 1000. Given the full contents of the code, this will always be true. This is ok, but a redundant if-check.
5. Inside the banana table, set whatever it is at index i * i / i to nil. Given that we start with i's value at 1, by the time we get here, the first time the formula evaluates to 2 * 2 / 2 = 2. So, set banana table's contents at index 2 to nil.
6. Check if i is larger than 5. If not, go back to 2.
(note: it's 3am, so pardon me if some details are wrong)
Ultimately, however, because you have the following:
Code: Select all
local queue = {}
local bananas = {}
...Nothing really happens. The if-check never evaluates to true in step 2, so the if-statement's contents are never executed, and setting random indices at step 5. accomplishes nothing. Effectively, this code is just repeating steps 3 through 6, accomplishing nothing of note, other than waste some CPU cycles.
However, if you are actually populating your tables with legible objects, I believe it's step 2.2 that is the source of your woes: do the tables or objects contained within the queue table actually contain the function "func"? You haven't posted any error traceback, or even code about how you might populate either queue or bananas table, so at best I can guess what your actual error is.
If I may be brutally honest, this code doesn't make much sense. Particularly the bit about "i * i / i", which you could just replace with "i". Was this AI generated, or what is the logic here? Please write what you actually aim to do with your code. No code, just words, explaining what your algorithm aims to do, though attaching any error traceback is also helpful.