Page 1 of 1

Accessing List of Globals in Love2D: nextvar not working

Posted: Tue Dec 20, 2011 11:13 am
by richard
Hi Folks,

I'm new to Love2D and Lua, but am having a lot of fun working with it.

I'm trying to write a function to restore game state from a file, and want to be able to search for object to set up by looking at the globals which are defined. I found this page:

http://www.lua.org/manual/2.4/node31.html

which suggests that nextvar can be used to iterate over the globals:

Code: Select all

function printGlobalVariables ()
  local i, v = nextvar(nil)
  while i do
    print(i)
    i, v = nextvar(i)
  end
end
But when I put this in my game code, I get the error:

Error: [string "pac.lua"]:1045: attempt to call global 'nextvar' (a nil value)

I'm not sure whether this is a Love2D environment issue, a Lua install/version issue, or that the documentation I've found is just wrong. Can anyone help?

Cheers,

Richard

Re: Accessing List of Globals in Love2D: nextvar not working

Posted: Tue Dec 20, 2011 12:10 pm
by Robin
That's the manual for Lua 2.4. The version LÖVE uses is 5.1.

That function has since been removed. As an alternative, you can just loop over _G:

Code: Select all

function printGlobalVariables ()
  for k in pairs(_G) do
    print(k)
  end
end

Re: Accessing List of Globals in Love2D: nextvar not working

Posted: Tue Dec 20, 2011 12:15 pm
by bartbes
Robin wrote:The version LÖVE uses is 5.2.
5.1

Re: Accessing List of Globals in Love2D: nextvar not working

Posted: Tue Dec 20, 2011 12:16 pm
by richard
Hi Robin,

That's brilliant, thanks! That works exactly how I need it to.

Cheers,

Richard

Re: Accessing List of Globals in Love2D: nextvar not working

Posted: Tue Dec 20, 2011 12:37 pm
by Robin
You're welcome!
bartbes wrote:
Robin wrote:The version LÖVE uses is 5.2.
5.1
Fucking typo. :(