Modules and Scope of variables

Hi guys,

I am building an application, and I am modularising it, to make it easy to maintain. Everything works fine, but I need some help or pointers in terms of accessing variables in different modules…

For example, I have an image on the screen… there is a button on the screen… when the button is pressed the image should dissapear… pretty easy?

The image is created in the main.lua file, the button is created in a gamefunctions.lua, which has the ontouch event… which is where the issue comes in… how do I access the image from main in gamefunctions.lua

Thanks in advance

Code posted

main.lua

[code]
local gameFunctions = require “gameFunctions”
local btnPressOK = true
local button

– Create New image
image1 = display.newImage(“reload.png”)

– Create button which hides and shows image
button = gameFunctions.createButtons()

[code]

gameFunctions.lua

[code]

module(…, package.seeall)

local ui = require(“ui”)

function createButton(originX, originY, sizeX, sizeY, imageBtnLocation,imageBtnPressedLocation)
local MenuBtn = ui.newButton{
defaultSrc = imageBtnLocation,
defaultX = sizeX,
defaultY = sizeX,
overSrc = imageBtnPressedLocation,
overX = sizeX,
overY = sizeX,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
MenuBtn.x = originX; MenuBtn.y = originY
return MenuBtn
end

function onbtnTouch()

print(“button Pressed”)
– when the button is clicked I want image1 to be hidden
– how do I access that variable from main

–main.image1.isVisible = false
end

function createButtons()
local btn = createButton(450, 300,48, 48, “next.png”, “next_p.png”)
btn:addEventListener(“tap”, onbtnTouch )
return btn
end

[code] [import]uid: 67619 topic_id: 15788 reply_id: 315788[/import]

@vik,
you might want to have a read of the following http://howto.oz-apps.com/2011/09/referencing-multiple-objects-of-same.html

and
http://howto.oz-apps.com/2011/09/scopes-for-functions.html

and
http://howto.oz-apps.com/2011/09/function-scope-answers.html

these should give you a better way to structure your code and make it modular.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15788 reply_id: 58306[/import]

hey mate,
thanks for that… Learnt some new things but it didn’t really explain how to access a variable in a different module…

in my original post the code looks like it is in the same file… I just reposted

its in the ‘onbtnTouch’ function…

main.lua

local gameFunctions = require "gameFunctions"  
local btnPressOK = true  
local button   
  
-- Create New image  
image1 = display.newImage("reload.png")  
  
-- Create button which hides and shows image  
button = gameFunctions.createButtons()  

gamefunctions.lua

  
module(..., package.seeall)  
  
local ui = require("ui")  
  
function createButton(originX, originY, sizeX, sizeY, imageBtnLocation,imageBtnPressedLocation)   
 local MenuBtn = ui.newButton{  
 defaultSrc = imageBtnLocation,  
 defaultX = sizeX,  
 defaultY = sizeX,  
 overSrc = imageBtnPressedLocation,  
 overX = sizeX,  
 overY = sizeX,  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }   
 MenuBtn.x = originX; MenuBtn.y = originY   
 return MenuBtn  
 end  
  
function onbtnTouch()  
  
 print("button Pressed")  
 -- when the button is clicked I want image1 to be hidden  
 -- how do I access that variable from main  
  
 --main.image1.isVisible = false  
end  
  
function createButtons()   
 local btn = createButton(450, 300,48, 48, "next.png", "next\_p.png")  
 btn:addEventListener("tap", onbtnTouch )  
 return btn  
end  
  

[import]uid: 67619 topic_id: 15788 reply_id: 58309[/import]

vik,

That’s because you are looking for something specific, not getting the broader/bigger picture.

  1. change the gamefunction.lua as following
function createButtons()  
 local function createButton(......)  
 end  
  
 local function onBtnTouch(event)  
 end  
  
 local btn = createButton(....)  
 btn:addEventListener("tap",onBtnTouch)  
 return btn  
end  

This encapsulates the function as one rather as three global functions that need not be accessed

  1. A module is generic, so in a module, you would never access the main module.
function createButtons(theImage)  
 local image = theImage  
  
 local function createButton(......)  
 end  
  
 local function onBtnTouch(event)  
 image.isVisible = not image.isVisible  
 end  
  
 local btn = createButton(....)  
 btn:addEventListener("tap",onBtnTouch)  
 return btn  
end  

now call this from the main as

local gameFunctions = require "gameFunctions"  
local btnPressOK = true  
local button   
  
-- Create New image  
image1 = display.newImage("reload.png")  
   
-- Create button which hides and shows image  
button = gameFunctions.createButtons(image1)  

what this will do is toggle the image on and off

Hope that helps,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15788 reply_id: 58310[/import]

Thanks mate… I do appreciate the assistance

[import]uid: 67619 topic_id: 15788 reply_id: 58312[/import]

viks,
did it work for you?

where are you from? you seem to be a new name on the forums.

cheers

?:slight_smile: [import]uid: 3826 topic_id: 15788 reply_id: 58314[/import]

Hi mate,

Yes it did… but I managed to get a work around using closures had to get something specific…

from melbourne in Australia… and yourself? I am guessing from the company name … from australia

Vik [import]uid: 67619 topic_id: 15788 reply_id: 58318[/import]

I’m from North QLD.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15788 reply_id: 58321[/import]