Page 1 of 1
Ellipsis not working?
Posted: Fri Feb 23, 2018 6:40 am
by khamarr3524
So, couldn't find anything (not counting the shape) besides a post from 2009 on the use of ellipsis (...) in Love2D. For some reason, when I invoke an ellipsis it doesn't pass the args I gave it, it's passing the args called when love is run, I.E the project directory and the -debug flag (I'm using ZeroBrane). I tried building it as a .love file and it is still giving the same results. Is this something in my code that's causing ellipsis to not function correctly, or is this an issue with Love?
Edit: Can confirm this is happening in a fresh project as well. The following code gives the same issue
Code: Select all
function aFunction(...)
print(arg[1] .. arg[2])
end
function love.load(dir, flags)
aFunction("Foo", "Bar", "123")
end
Edit2: This does seem to be an issue with love, if i add arg = {} to love.load it causes the arg table to be empty and invoking another function using ellipsis does not work at all.
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 6:53 am
by PGUp
Like this ?
Code: Select all
function aFunction(...)
local arg = {...}
love.graphics.print(arg[1] .. arg[2] .. arg[3])
end
function love.draw(dir, flags)
aFunction("Foo", "Bar", "123")
end
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 6:55 am
by grump
Code: Select all
print(...) -- top level, outside functions: contains the current module name
function foo(...)
print(...) -- function level: vararg expression, contains (extra) arguments passed to function
end
print(unpack(arg)) -- global table arg: contains command line arguments
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 6:58 am
by khamarr3524
That's a workaround that will work, but there is still an underlying cause as to why Love is hijacking the ellipsis that would be interesting to find out what's causing it.
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 7:11 am
by grump
It's not hijacking anything. You keep confusing ellipsis and the global arg table. They're entirely different things.
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 7:18 am
by khamarr3524
Unless there was a change (which is quite likely) from Lua 5.0 to 5.2 concerning ellipsis, lua default populates the global arg table with ellipsis, as seen
here. Although it would make sense if Love2d is intentionally using the global arg table to retain the .exe args which is what you are saying.
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 7:22 am
by grump
PIL is outdated in this regard. 5.1 changed how ellipsis and arg are used. See my first comment, that's how they work in Lua 5.1.
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 7:37 am
by zorg
Also, table.getn is replaced by the # operator, and the module keyword is deprecated.
And löve does use luaJIT by default, which is a combination of 5.1 plus some additional 5.2 stuff that doesn't interfere with how 5.1 did things.
Re: Ellipsis not working?
Posted: Fri Feb 23, 2018 8:17 am
by pgimeno
It's actually a difference between Lua and LuaJIT.
Lua 5.1 still recognizes arg, but LuaJIT doesn't. See
http://luajit.org/faq.html (third item).
Code: Select all
$ lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> function x(...) for k,v in next,arg do print(k,v) end end; x(1,2,3)
1 1
2 2
3 3
n 3
>
$ luajit
LuaJIT 2.0.4 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> function x(...) for k,v in next,arg do print(k,v) end end; x(1,2,3)
stdin:1: bad argument #1 to '(for generator)' (table expected, got nil)
stack traceback:
[C]: in function '(for generator)'
stdin:1: in function 'x'
stdin:1: in main chunk
[C]: at 0x557ebb8d16c0
> =arg
nil
>
Re: Ellipsis not working?
Posted: Sat Feb 24, 2018 9:06 am
by khamarr3524
That makes sense. I think I'm going to move away from ellipsis for a different reason, but at least I know now how if I want to do it I can. Thanks for the clarification @pgimeno