I have read the “Goodbye Globals!” tutorial. Even though I thought I understood it all, I can not make it work. What am I doing wrong?
I took the very simple “TapTouch” tutorial to try globalData.lua.
So I have main.lua where I initiate the playerName value.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local trial = require ( "trial" ) local globalData = require( "globalData" ) globalData.playerName = "John Smith" -- Your code here local backObject = display.newRect( 130, 130, 150, 150 ) backObject:setFillColor( 0.2, 0.4, 0.8 ) backObject.alpha = 0.75 backObject.name = "Back Object" local frontObject = display.newRect( 190, 190, 150, 150 ) frontObject:setFillColor( 1, 0.2, 0.3 ) frontObject.alpha = 0.75 frontObject.name = "Front Object" -- Tap listener function local function tapListener( event ) local object = event.target print( object.name .. " TAP" ) end -- Touch listener function local function touchListener( event ) local object = event.target print( object.name .. " TOUCH (" .. event.phase .. ")" ) end -- Add event listener to back object backObject:addEventListener( "tap", tapListener ) -- Add event listener to front object frontObject:addEventListener( "tap", tapListener )
Then I have globalData.lua:
- Pseudo-global space local M = {} return M
Then I wrote a trial.lua:
-- trial.lua local globalData = require( "globalData" ) local currentPlayer = globalData.playerName print("currentPlayer",currentPlayer)
And when I run it, I get nil for playerName:
Oct 17 02:03:36.439 Loading project from: ~/Desktop/TapTouch Oct 17 02:03:36.439 Project sandbox folder: ~/Library/Application Support/Corona Simulator/TapTouch-72075831EDC2B714C4641252F8217757 Oct 17 02:03:36.439 Platform: iPad / x86\_64 / 10.12.1 / Intel HD Graphics 4000 OpenGL Engine / 2.1 INTEL-10.20.25 / 2017.3184 / en-US | US | en\_US | en Oct 17 02:03:36.505 currentPlayer nil
Thank you for any help on this.