Can't access variables from another module

I am creating a clean up module (cleanup.lua) so I can use it in level modules.

Example:

_ level1.lua _ has local variables that needs to be cleaned after gameover so I attached _ cleanup.lua _ at the end in _ level1.lua _ using require.

_ cleanup.lua _ is being loaded correctly but can’t access variables in _ level1.lua _.

I was trying to use _ cleanup.lua _ as an include file but I guess it works differently.

So my questions are:

  1. How do I access the local files in _ level1.lua _ from _ cleanup.lua _ WITHOUT MAKING THEM GLOBAL?
  2. Do I need to clean the local variables in _ level1.lua _ manually (ie. setting to nil), or will they cleaned automatically when I switch to a different scene (using Director)?

Thanks!

[import]uid: 39031 topic_id: 22455 reply_id: 322455[/import]

If you are using director you really don’t need an external cleanup module. Director has a built in cleaning function that is called by putting in function clean() at the end of your level1.lua file and putting what ever u want to clean in that function. for example:
[lua]–Level.lua file
module(…, package.seeall)
function new()
local newScene = display.newGroup ()

–LEVEL CODING HERE–

function clean ()

–PUT ANYTHING YOU WANT TO CLEAN UP IN THIS FUNCTION AND DIRECTOR WILL CALL IT, MAKE SURE ITS A GLOBAL FUNCTION.

Runtime:removeEventListener (“enterFrame”, listener)

if sound then
audio.dispose (sound)
sound = nil
end
end
return newScene
end[/lua]
i leaned how to use director and th clean function from looking at the ghost vs. monsters example. it is an invaluable tool.
[import]uid: 126161 topic_id: 22455 reply_id: 89586[/import]

For what it’s worth, here’s my opinion, but it’s not an off-the-shelf solution.

Coming from other programming languages I struggled hard with my local vars not being visible in other modules, and using global vars and G_ stuff. I was never really happy with the way things worked until I started getting smart and reorganised my code to work WITH Lua style coding instead of against Lua style coding.

It’s hard to explain, but it just took me a while to structure my code in a way that mirrors the way my screens and menus pop-up. And it works nicely now! Either way, you shouldn’t be passing around huge chunks of data between modules, in my opinion. Pass along a high-score or similar stuff as a global value, but not much more (you can localise these globals if you need to perform a lot of logic on them if needed).

Well, that’s it. A bit of a vague pointer but I’d say if you feel you’re fighting with globals and locals not being visible among modules: restructure your code. You can usually do with a lot less modules than you think. And keep separate modules for screen that are also completely separate in the game.

Good luck,
Thomas [import]uid: 70134 topic_id: 22455 reply_id: 89614[/import]

if you want to used that other module functions in 2nd module then you must used like this code

seconemodule = rewuire “secondmodule”

seconemodule .functionname(params)

this is a simple way to use the module code :-p
[import]uid: 107779 topic_id: 22455 reply_id: 89622[/import]

What I am trying to do is create one file to store all variables, timers, event listeners cleaning job and attach that file to the level files so I don’t have to edit 100 level files when I add new variables to clean. (just like how Include is done).

@3Dreams
I read somewhere that clean function was removed from 1.4. Also, it looks like you’re creating a function called Clean() in your example? Guess I will take a look at ghost vs. monsters.

@thosmas
Thanks. I start out with a mindset of never using globals but the temptation of the ease of use it too much at times!

@syed
Yes, that’s how I am connecting the module but it still won’t allow the “secondmodule” to access local variables in the main code. [import]uid: 39031 topic_id: 22455 reply_id: 89642[/import]

The clean function that I create at the end is automatically called by director because it is not localized. The version I am using is the one that came with ghost vs monsters and It works great, I have 60 levels currently and another 60 in the works.

As far as globals go, they are not evil, they are great to use as long as you properly nil them out after use. I use 4 per level that is accessed by my mainGame module that stores all the info that does not change for each level. And in they are nilled out with the director clean function.

Another way you can do it is the way that I access all my modules, I’m on my phone rift now so I cannot find the link but basically you make a table at the beginning of the module then you add each function or variable into the table and return the table at the end of the module. This is very easy and saves you from having to have a lot of globals.

I can give u an example when I am around a computer later. [import]uid: 126161 topic_id: 22455 reply_id: 89656[/import]

Ah, so you’re saying there is no clean function defined in Director, but there is a line calling clean() function when switching scenes? [import]uid: 39031 topic_id: 22455 reply_id: 89672[/import]

Hi, You should not use “module(…, package.seeall)” it is on it way down the drain :slight_smile:

main.lua

local anotherLua = require( "another");  
local master = {};  
  
local function printValue(value)  
 --Getting this from the another.lua  
 print(value);  
end  
master.printValue = printValue;  
  
anotherLua.setTarget(master);  
anotherLua.getValue("\*\* Passes this to the another.lua \*\*")  
  

and the another.lua

local myModule = {};  
local t = {};  
  
function setTarget(t)  
 target = t;  
end  
myModule.setTarget = setTarget;  
  
getValue = function( val)  
  
 --Send it back to main.lua  
 target.printValue(val)  
end  
myModule.getValue = getValue;   
  
return myModule;  

regards, Joakim [import]uid: 81188 topic_id: 22455 reply_id: 89690[/import]

iolo72@ yes when you change scenes with director anything u put in the clean function will be cleaned at scene change.

jkrassman give u an example of what i was talking about earlier , but im home now so ill give you another example of a great way to access modules.

[lua]–gameFunctions.lua module
–First create a table to house all of the modules functions
local gameTable = { }

–Then create a local function
local function createRect ()
local ball = display.newRect ( 0, 0, 100, 50)
ball.x = display.contentWidth; ball.y = display.contentHeight;
end
–After you create the function then insert it into the table for the module
gameTable.createRect = createRect

–you can even do variables like this
local firstBoolean = true
–then insert the variable into the table
gameTable.firstBoolean = firstBoolean

–After you have inserted all functions and variables you want to access from outside modules then you return the table at he end of module like this

return gameTable[/lua]

now we go to the level module
[lua]–level module or any other module u want to access the gameFunctions module

–it is very easy to access the information on the other module
–just require in the module
local gameFunctions = require (“gameFunctions”)

–then create local variables to pull the info from the table
local createRect = gameFunctions.createRect
–Now you can use that function from a different module.
createRect()

–Same goes for the boolean, with one exception
local firstBoolean = gameFunctions.firstBoolean
–this will grab the current state of firstBoolean variable and use or display it, you cannot change the state of the boolean in the module unless u access it like this

print (gameFunctions.firstBoolean)

gameFunctions.firstBoolean = false

–Now u can change the state of the module variable by addressing it by its full name[/lua]

i hope this helps, i read a blog or a post on this and it helped me big time, it gets rid of the whole package.seeall deal
[import]uid: 126161 topic_id: 22455 reply_id: 89720[/import]

Thanks for the reply, guys!

FYI, Ricardo removed the clean group call from v1.4 so I will be using the one I already have in each level. [import]uid: 39031 topic_id: 22455 reply_id: 89751[/import]

iolo72, according to his website http://rauberlabs.blogspot.com/2011/08/director-14-books.html, it says “- clean() function is back” for director 1.4. just saying, cause i use 1.4 and i use the clean function. [import]uid: 126161 topic_id: 22455 reply_id: 89832[/import]

Thanks for letting me know.
I guess he didn’t update the comment section in the code as it indicates that cleanGroups() has been removed on 6/21/2011. [import]uid: 39031 topic_id: 22455 reply_id: 90070[/import]