This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Nothy's profile picture

How does one access the global table "_G"?

Started by Nothy, 17 May 2016 - 07:27 AM
Nothy #1
Posted 17 May 2016 - 09: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. :)/>
Bomb Bloke #2
Posted 17 May 2016 - 10: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.
Nothy #3
Posted 17 May 2016 - 11:13 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.
Thank you! I found exactly what I was looking for :)/>
And yes, getfenv() did do the trick for me :D/>