Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring global variable inside a function in LUA

Tags:

lua

I have a function inside which I declared a global variable obs, inside a function and assigned some value.If I want to access this in some other lua file, it gives an error: "attempt to call obs a nil value, what do I need to do to able able to access it?

Here is the dummy code for it

//A.lua
function X()
obs = splitText(lk,MAXLEN)
end

//B.lua
function Y()
for i=1, #obs do      //error at this line
end
end
like image 781
S Khurana Avatar asked Jun 20 '14 12:06

S Khurana


Video Answer


2 Answers

There are a few ways to do this. With your current setup, you could do this:

a.lua

function x()
    -- _G is the global table. this creates variable 'obs' attached to
    -- the global table with the value 'some text value'
    _G.obs = "some text value"
end

b.lua

require "a"
function y()
     print(_G.obs); -- prints 'some text value' when invoked
end

x(); y();

Stuffing things in the global table is usually a terrible idea though, as any script anywhere else could potentially overwrite the value, nil out the variable, etc. a much better way imo would be to have your a.lua return its functionality in a table which you can capture in files which require it. this will allow you to define a getter function to return the 'obs' variable attached directly to your 'a.lua' functionality in its current state.

you probably want to do something like this for better portability (it is also much clearer which modules define which functionality this way):

a.lua

local obs_
function x()
    obs_ = "some text value"
end

function get_obs()
    return obs_
end

return { x = x, obs = get_obs }

b.lua

local a = require "a"
function y()
    print(a.obs())
end


a.x()
y()

since you mentioned you can't use require, i'll assume you're working in some other framework that uses some other function to load libraries/files. in that case, you will probably have to just stuff everything in the global table. maybe something like this:

a.lua

-- this will attach functions a_x and a_get_obs() to the global table
local obs_
function _G.a_x()
    obs_ = "some text value"
end

function _G.a_get_obs()
    return obs_
end

b.lua

-- ignore this require, i'm assuming your framework has some other way loading
-- a.lua that i can't replicate
require "a"

function y()
    print(_G.a_get_obs())
end


_G.a_x()
y()
like image 174
Mike Corcoran Avatar answered Sep 18 '22 11:09

Mike Corcoran


Remember that some Lua programs inside other programs (Garry's Mod, World of Warcraft, Vera, Domoticz) uses _ENV instead of _G to limit their scope. So global variables has to be:

_ENV.variable = 1

instead of:

_G.variable = 1

The reason why this happens is because the developer wants to limit the standard Lua library to avoid that the user access methods like: os.exit().

To see if _ENV is used instead of _G, print it out and if it returns a table instead of nil, it's most likely used. You can also test with the following snippet:

print(getfenv(1) == _G)
print(getfenv(1) == _ENV)

Where the one to print true is the one you are using.

like image 32
Mossarelli Avatar answered Sep 22 '22 11:09

Mossarelli