Page 1 of 1

Bug with arg[]

Posted: Mon Mar 17, 2014 8:49 pm
by ilgamer
When I am using this construction:

Code: Select all

function foo(...)
    print(arg[1])
    print(arg[2])
end

function love.load()
  foo("sample", 123)
end
It prints something really weird. In the first case it prints the path to the project: "C:\Projects\ProjectFolder" and in the second case it is nil. Somebody had this bug?

Re: Bug with arg[]

Posted: Mon Mar 17, 2014 8:54 pm
by Nixola

Code: Select all

function foo(...)
    local arg = {...}
    print(arg[1])
    print(arg[2])
end

function love.load()
  foo("sample", 123)
end
Make it like this. Arg is an implicit deprecated variable, you need to declare it explicitly before using it

Re: Bug with arg[]

Posted: Mon Mar 17, 2014 10:08 pm
by slime
As Nixola mentioned, 'arg' as an implicitly created table for var-arg parameters in functions was deprecated in Lua 5.1 (2006). Unfortunately the official online version of the Programming in Lua book is written based on Lua 5.0, so it doesn't tell you that.

Better to not use the name 'arg' at all, since you'll be shadowing the global 'arg' table (which contains command-line arguments passed to love.)

Code: Select all

function foo(...)
    local args = {...}
    print(args[1], select(1, ...), args[1] == select(1, ...))
end

Re: Bug with arg[]

Posted: Mon Mar 24, 2014 2:07 am
by ilgamer
Thank you!