Jump to content




How does one access the global table "_G"?


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

#1 Nothy

  • Members
  • 249 posts
  • LocationMars

Posted 17 May 2016 - 08:27 AM

I feel like this should be really simple, but I cannot find how to do it.
Basically what I want to do is make a program that prints out every item in the table for debugging.

I'll appreciate every bit of help I can get :D
Thanks in advance. :)

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 17 May 2016 - 09:30 AM

You "access" _G the same way you do any table; just type in the name of the variable holding the pointer. In this case... it's "_G".

If you want to list out all its contents, then I advise using a file for the purpose, as it's rather crowded in there:

local output = fs.open("_G.txt", "w")

for key, value in pairs(_G) do
  output.writeLine(key .. ": " .. tostring(value))
end

output.close()

Though, is it really _G you want to be viewing? ComputerCraft scripts don't use it as their environment table, by default! You may be better off inspecting the table returned by getfenv(), instead.

#3 Nothy

  • Members
  • 249 posts
  • LocationMars

Posted 17 May 2016 - 10:13 AM

View PostBomb Bloke, on 17 May 2016 - 09:30 AM, said:

You "access" _G the same way you do any table; just type in the name of the variable holding the pointer. In this case... it's "_G".

If you want to list out all its contents, then I advise using a file for the purpose, as it's rather crowded in there:

local output = fs.open("_G.txt", "w")

for key, value in pairs(_G) do
  output.writeLine(key .. ": " .. tostring(value))
end

output.close()

Though, is it really _G you want to be viewing? ComputerCraft scripts don't use it as their environment table, by default! You may be better off inspecting the table returned by getfenv(), instead.
Thank you! I found exactly what I was looking for :)
And yes, getfenv() did do the trick for me :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users