Lua Code cannot be recursive

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
little box
Posts: 5
Joined: Sun Jan 05, 2020 5:24 am

Lua Code cannot be recursive

Post 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?
User avatar
Tony Li
Posts: 22163
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Code cannot be recursive

Post 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
Post Reply