Jump to content

Get code acces to local Vars

5 replies to this topic

#1 Wilma456

  • Members
  • 152 posts
  • LocationGermany

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.

#2 Lupus590

  • Members
  • 1,739 posts
  • LocationUK

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.

#3 Sewbacca

  • Members
  • 409 posts
  • LocationStar Wars

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 by Sewbacca, 04 March 2017 - 10:37 PM.

#4 KingofGamesYami

  • Members
  • 2,865 posts
  • LocationUnited States of America

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.

#5 KidBrine

  • Members
  • 38 posts
  • LocationHoquiam Wa

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 by KidBrine, 27 January 2017 - 12:42 AM.

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 6,732 posts
  • LocationTasmania (AU)

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.

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users