Jump to content




List All Variables

lua

  • You cannot reply to this topic
3 replies to this topic

#1 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 28 February 2013 - 10:29 PM

In a project I am making I need to run an separate Lua file at the same time as the current one. This is working fine using os.run(...). However, I am having a bit of trouble with the environment. I need the separate Lua file to access all the variables and functions in the main file. I could add all of the functions to the environment one by one, but there must be another way.

I've looked at _G but that doesn't seem to work.

For example, the following code list all the standard variables and functions (e.g. 'os', 'math', etc) but not 'aFunction' or 'aVariable'. What can I use to get them.

aVariable = "Hello"


function aFunction (message)
	print(message)
end

for key, value in pairs(_G) do
	print(key)
	print(value)
end

If you have a better idea on how to add all the functions to the environment please suggest it.

#2 GopherAtl

  • Members
  • 888 posts

Posted 01 March 2013 - 12:22 AM

getfenv() returns the current function environment, which will contain all non-local variables and functions you've declared in your program.

#3 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 01 March 2013 - 01:47 AM

in you current file use

os.run(getfenv(),"thesecondfile")

and the second file will have access to all GLOBAL variables from the first file. there is no way to access local variables in a different file

#4 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 01 March 2013 - 01:21 PM

View PostKaoS, on 01 March 2013 - 01:47 AM, said:

in you current file use

os.run(getfenv(),"thesecondfile")

and the second file will have access to all GLOBAL variables from the first file. there is no way to access local variables in a different file


View PostGopherAtl, on 01 March 2013 - 12:22 AM, said:

getfenv() returns the current function environment, which will contain all non-local variables and functions you've declared in your program.

Thanks both of you, worked a charm.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users