global variable not seen by other code with director lib

A global variable I define is not being seen by some of my code, and I don’t know why. Can somebody look at this and tell me what I’m doing wrong… Thank you!

What would be a better way of propagating the data to all the code of my project?

-- main.lua:  
  
display.setStatusBar( display.HiddenStatusBar )  
  
director = require("director")  
  
-- here's the global variable that nobody sees outside of this file  
test = "SUPERTEST"  
  
function button\_press( event )  
 if event.phase == "ended" then  
 director:changeScene(event.target.scene,"fade")  
 end  
end  
  
local mainGroup = display.newGroup()  
  
local function main()  
 mainGroup:insert(director.directorView)  
 director:changeScene("title")  
 return true  
end  
  
main()  
-- title.lua  
  
module(..., package.seeall)  
  
local localGroup = display.newGroup()  
  
local title = display.newText( localGroup, "all i'm gonna do is change a 'global'", 0, 0, native.systemFontBold, 24 )  
title.scene = "print\_test"  
  
title:addEventListener( "touch", button\_press )  
  
-- here's where I change the global variable  
test = "SUPER DUPER TEST"  
  
function new()  
 return localGroup  
end  
-- print\_test.lua  
module(..., package.seeall)  
  
local localGroup = display.newGroup()  
  
-- this uses the original test value :-(  
local title = display.newText( localGroup, "test is "..test, 0, 0, native.systemFontBold, 24 )  
  
function new()  
 return localGroup  
end  

[import]uid: 61132 topic_id: 10807 reply_id: 310807[/import]

Well, I guess I figured it out. I created a fourth file called test.lua, and defined test in there, then required it from main and assigned it to test. Now I reference test.test and it works fine.

WEIRD! (?) :slight_smile:
[import]uid: 61132 topic_id: 10807 reply_id: 39251[/import]

@coronasdk730,
you can also try using my propertyBag library, that can create equivalent of UserDefaults in memory and can be persisted to disk.

it is easy to use. have a look at

http://howto.oz-apps.com/2011/03/launch-rating-dialog-in-game.html
and
http://howto.oz-apps.com/2011/03/propertybag-for-all-your-data.html

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10807 reply_id: 39256[/import]

to add to that last comment,
think of it as you can set a variable called level and then save it to disk for the next time it is launched.

[lua]unlocked = tonumber(pBag.getProperty(“Unlocked”,1))
lives=tonumber(pBag.getProperty(“Lives”,3))
Level=tonumber(pBag.getProperty(“Level”,1))
stage.saveDefaults()[/lua]

not only will it be available in all of the modules but also be able to save and load from the device.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10807 reply_id: 39258[/import]

jayantv, that looks like a useful library! Can you load/save tables? [import]uid: 61132 topic_id: 10807 reply_id: 39264[/import]

@coronasdk730,
not at this time, it works only with strings and integers. I have not got round to fixing it for booleans and others like tables.

so for now, if you get a value from it, try to use the toNumber to convert it to a number if you want numbers.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10807 reply_id: 39265[/import]

A bit late in the discussion, but if you called “test” “_G.test” instead, it should work in director.

I’m not using the latest version of director, but previously adding the _G. solved my problem.

Peach :slight_smile: [import]uid: 52491 topic_id: 10807 reply_id: 39320[/import]

@peach, thank you for the clue. I’ll check that out when I get a chance. I did try referencing it with main.test (since it was defined in main.lua), but that didn’t work. The way I ended up doing it was more modular, which we both can appreciate, right? :smiley:
[import]uid: 61132 topic_id: 10807 reply_id: 39328[/import]

I do like modular :wink:

But indeed, give it a go - it does work for me and I find it convenient.

Peach :slight_smile: [import]uid: 52491 topic_id: 10807 reply_id: 39448[/import]

It’s worth pointing out that if you declare variables in main() that you wish to be accessible in other modules, you must put the declaration of those variables BEFORE the require() statements. I suppose it may be obvious, but I’d gotten in the habit of always placing requires before everything else and it threw me for a while.

Also - with reference to using globals - you will always see folks who will preach that you should never use globals and that they are the devil’s spawn. The fact is that any moderately complex application or better is going to require globally accessible data to be stored in one way or another. So what should you do?

Globals are “bad” for two reasons - first, they make it possible for a bug in one part of your app to affect a completely different part of your app. The upshot: Too many globals can make it tough to track down bugs.

In Lua, the second reason (and one most often sited) is performance - obviously a major concern with real-time games. However solutions like the property bag (going to the file system) or storing info in SQLLite don’t help on either front. They are still global, so debugging is still impacted. And writing to the file system or going to a database is guaranteed to be slower - probably much slower than global variables. Property bag/SQLLite CAN be very useful for top-ten lists and other info that should be saved from one run to the next. But for standard global variables _G is the way to go - just be sure to copy the _G variables you’ll use in a module to local variables before you use them. That will help dramatically with any speed issues.
[import]uid: 35573 topic_id: 10807 reply_id: 39819[/import]