Page 1 of 1

Lua Code cannot be recursive

Posted: Sun Jan 05, 2020 5:52 am
by little box
Hi!
I tried to write some data into lua and read it out in code. But it looks like the lua code doesn't support recursion.
Here's my example:
function TestForEach(v, n)
local sum = 0;
if n > 0 then
sum = v + TestForEach(v, n-1);
end
return sum;
end
print(TestForEach(3, 3));
It should be 9 but it's 0.
plugin version: 2.2.1
What can i do?

Re: Lua Code cannot be recursive

Posted: Sun Jan 05, 2020 9:09 am
by Tony Li
It looks like there might be an issue with locals in this Lua implementation. I'll check that for the next release (v2.2.5). In the meantime, you can just change it to this:

Code: Select all

function TestForEach(v, n)
    if n > 0 then
        return v + TestForEach(v, n-1)
    end
    return 0
end