Project global variables?

If I declare a global variable in main.lua can I update that variable from inside a function from another module. It doesn’t seem to retain the value.

Is there a way to have project wide global variables?

How can I do this?

[import]uid: 9371 topic_id: 3822 reply_id: 303822[/import]

Or should I create a options module with local variables and set up functions that can set and get the local values [import]uid: 9371 topic_id: 3822 reply_id: 11610[/import]

use _G.

that is always global

eg _G.something = “whatever”

will always change it wherever you have it

[import]uid: 6645 topic_id: 3822 reply_id: 11613[/import]

Wooo! Thanks. But I’ve now setup a module with Get and Set functions, I think that’s better no? [import]uid: 9371 topic_id: 3822 reply_id: 11614[/import]

OK this is what I’ve done, perhaps it will be useful for others…

  1. Create an options.lua like this:

[blockcode]

module(…, package.seeall)

local opt_sounds = “off”
local opt_difficulty = “easy”

function options:setOption(option, value)

if (option == “sounds”) then
opt_sounds = value
elseif (option == “difficulty”) then
opt_difficulty = value
end

print("options.lua, options:setOption, option = “…option…” value = "…value)
end

function options:getOption(option)

print("options.lua, options:getOption, option = "…option)

if (option == “sounds”) then
return opt_sounds
elseif (option == “difficulty”) then
return opt_difficulty
end
end
[/blockcode]

  1. Then in main.lua do this:

[blockcode]

local options = require(“options”)

[/blockcode]
3) In my other modules I can now get like this:

[blockcode]
local opt_difficulty = options:getOption(“difficulty”)
[/blockcode]

and set like this:

[blockcode]
options:setOption(“difficulty”, “hard”)
[/blockcode]
Not too sure if there is a better way but it works for me! Any thoughts… [import]uid: 9371 topic_id: 3822 reply_id: 11618[/import]

for what you’re doing yes.

i was thinking more if you were just storing a single global variable for something eg

_G.fps = 60

[import]uid: 6645 topic_id: 3822 reply_id: 11628[/import]

dweezil and jmp909,

I am at a crossroad trying to decide which method to us. Global Module w/ local variables or accessing Global variables directly.

Are either of you familiar to which method is faster on a device? I will need to read/write fairly often and want to make sure I choosing the most efficient method from the start.

Thanks! [import]uid: 9187 topic_id: 3822 reply_id: 12408[/import]

In the docs it states:

Lua: Best Practices

Simple changes to your Lua code can yield tremendous benefits. Below are some examples of ways to squeeze out extra performance in your Lua code using very simple coding changes:
Use locals (i.e. avoid global variables)

Avoid global variables. Period. In Lua, you will sacrifice performance if you use global variables. When in doubt, precede your variable declarations with local .

See here : http://developer.anscamobile.com/content/performance-and-optimization [import]uid: 9371 topic_id: 3822 reply_id: 12426[/import]

Thanks for the info. I’ve actually read through that.

I just don’t see any other way to pass app wide information such as a score from level to level without making SOMETHING global. And in this doc is says “In Lua functions are variables too”.

So using a global function to manipulate a local variable is just as costly as manipulating a global variable?

I am pretty new at all of this so I am probably missing something, but which one of the methods mentioned above for handling globals worked best for you? [import]uid: 9187 topic_id: 3822 reply_id: 12431[/import]

I’m using the example code I posted in post number 4. Interesting point about global functions… [import]uid: 9371 topic_id: 3822 reply_id: 12432[/import]

Global Variable vs. Global Function

I’m not sure if this is the best or most accurate way to test. But thought you might still be interested anyway.

Local Only (Control Group) - 4sec in Simulator: main.lua
[lua]–local test = require(“speedTest”)

local x = 0
–x = 0

print(os.time())
while x < 100000000 do

–x = test:set()
–_G.x = _G.x+1
x = x+1

end
print(x)
–print(_G.x)

print(os.time())[/lua]

Global Variable Test - 25sec in Simulator: main.lua
[lua]–local test = require(“speedTest”)

–local x = 0
x = 0

print(os.time())
while x < 100000000 do

–x = test:set()
_G.x = _G.x+1
–x = x+1

end
–print(x)
print(_G.x)

print(os.time())[/lua]

Global Variable without _G - 15sec in Simulator: main.lua
[lua]–local test = require(“speedTest”)

–local x = 0
x = 0

print(os.time())
while x < 100000000 do

–x = test:set()
–_G.x = _G.x+1
x = x+1

end
print(x)
–print(_G.x)

print(os.time())[/lua]

Global Function Test - 17sec in Simulator: main.lua
[lua]local test = require(“speedTest”)

local x = 0
–x = 0

print(os.time())
while x < 100000000 do

x = test:set()
–_G.x = _G.x+1
–x = x+1

end
print(x)
–print(_G.x)

print(os.time())[/lua]
speedTest.lua
[lua]module(…, package.seeall)

local x = 0
function speedTest:set()

x = x+1
return x
end[/lua]

I have never been able to access global variables outside of my main.lua without _G, so I’m not sure that test even matters but its good for reference. I guess using your Global Function to set Local Variable is the fastest way. Thanks! [import]uid: 9187 topic_id: 3822 reply_id: 12533[/import]

OK this is what I’ve done, perhaps it will be useful for others…

Just wanted to say thanks. This was useful for me. Is this still an okay way to handle it or has something better been found?

  • Aaron
    [import]uid: 27966 topic_id: 3822 reply_id: 20086[/import]

Aaron - I am using dweezil’s global function method and found that it works great. [import]uid: 9187 topic_id: 3822 reply_id: 20089[/import]