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

Get code acces to local Vars

Started by Wilma456, 11 January 2017 - 03:34 PM
Wilma456 #1
Posted 11 January 2017 - 04:34 PM
I want to code a plugin system and it need acces to the local vars of my programm. I had tried it with load(), but it don't have acces.
Lupus590 #2
Posted 11 January 2017 - 05:43 PM
so you want your plugins to share the enviroment of your program?

I'm not 100% sure, but I think you can put everything into a table and use the table as the enviroment, how you do that I don't know.
Sewbacca #3
Posted 11 January 2017 - 11:03 PM
Os.run() provides a similar possibility (No access to locals! It is just possible out of Lua or with the debug API to access them (CCTweaks support it)), you can put vars into a table and share it for your programs (They will be able to access _G). Otherwise you can create a table like os.run, with more possibilities (loadfile(setmetatable({<Your vars>}, { __index = _G –[[To allow the program to use APIs]]})). If you are new at environments, i would recommend to look at the Lua manual. Sorry for so manny links. I am just in a link hype ^^.
Edited on 04 March 2017 - 09:37 PM
KingofGamesYami #4
Posted 11 January 2017 - 11:13 PM
You can't do that. Even if you could, it would be a bad idea.

A better idea would be to pass the variables as arguments.
KidBrine #5
Posted 27 January 2017 - 12:40 AM
Here is how you it's done.
I have some example vars.
First program
local Var1 = "i'm first"
local Var2 = "i'm second"
local Var3 = "and i'm third"
vars = {Var1,Var2,Var3}
Second Program
local Var1 = vars[1]
local Var2 = vars[2]
local Var3 = vars[3]
-- not rquired just so that they are used
print(Var1) -- should write "i'm first"
print(Var2) -- should write "i'm second"
print(Var3) -- should write "and im third"
Edited on 26 January 2017 - 11:42 PM
Bomb Bloke #6
Posted 27 January 2017 - 01:10 AM
That won't work. You won't have access to the original variables, but rather just to the copies you placed into the "vars" table. Altering those won't alter the original locals.