Variable Amounts of Parameters

I’m having trouble getting functions with a variable amount of parameters to work correctly.

When i write a function like

function testArg(...)
    print(arg[1])
end

and then call it with

testArg(1,2,3)

I get an error that says “Attempt to index a nil value (global ‘arg’)”

Normally I would just assume that I didn’t do something right, but then I also found that the Roller Coaster project also has a function with variable parameters, so I replaced its main with

a = Vec3(1,2,3)
Vec3.SetEye(1,2,3)

and this gives the same error.

I just learned how to write these types of functions yesterday, so it may be that I’m doing something wrong, but it seems odd that the function in an example project would also not work (or maybe I’m calling it incorrectly).

My main question is: how can I correctly write and use a function with a variable amount of parameters in Codea?

Thank you.

Changes to Lua require you to do this…

function foo(...)
    -- get the number of args (if you need it)
    local n = select('#', ...)

    -- make a table out of the args
    local arg = {...}

    --now it works as you expect
    if n > 0 then
        print(arg[1])
    end
end

Oh! Thank you!