Bug with arg[]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
ilgamer
Prole
Posts: 10
Joined: Fri Apr 12, 2013 5:55 am

Bug with arg[]

Post 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?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Bug with arg[]

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Bug with arg[]

Post 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
ilgamer
Prole
Posts: 10
Joined: Fri Apr 12, 2013 5:55 am

Re: Bug with arg[]

Post by ilgamer »

Thank you!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests