I see, you have different .lua files. Then you need this:
http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Rob
I see, you have different .lua files. Then you need this:
http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Rob
Thaks Rob…I have read this 2 times…
I read it right now very carefully one more time…
I still don’t get it.
--my global space local M = {} return M
I don’t get this – return M
in my case I have
local dots = { } – the same as – local M = { }
so I now should do this… return dots…but where?
local dots = {} -- here? -- return dots function newDots (group) -- or here.... local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY - 110 local distance = 30 for i=1, 10 do -- or here? dots[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert(dots[i]) dots[i].x = initialX + (separacion\*i) dots[i].y = initialY + distance\*1 dots[i]:scale(.5, .5) end -- maybe here.....? end -- or at the very end....?
and why Do I need the return dots…?
And then after that… in my other Module… or file.lua (guitar.lua)
how do I access the variable dots?
newDots(group) --dots[4].x = 100
you see? I don’t get it…
Honestly Victor, it’s hard to explain it much better than the tutorial, but I will try.
In Lua when you do:
local somemodule = require (“somemodule”)
It opens somemodule and whatever that .lua file return s at the end, becomes the value of the variable somemodule. If you do:
local facebook = require(“facebook”)
you end up with a variable facebook, which is a table that holds all of the facebook methods and attributes it needs to work like facebook.request(), etc. It’s just a lua table. In this case, mydata.lua contains an empty table:
local M = {} return M
and returns that empty table. When in your code, you do:
local mydata = require(“mydata”)
then mydata is an empty table. Of course you could pre-populate values in mydata.lua:
local M = {} M.soundOn = true M.level = 1 return M
in which case the table wouldn’t be empty but have a couple of entries already. Ergo:
local mydata = require(“mydata”)
print(mydata.level)
would print “1” to the console. In this case, mydata becomes the M table from the mydata.lua file. For this to work, you also have to understand that when you require a module, it only gets loaded once, regardless of the number of times you do it. In other words, the first time you require(“mydata”) the code in that module executes and the table is returned. In a later scene, if you do the require again, all you get is a reference to the table so you can access it from the scene, but the contents of mydata are just as they were in the other scenes.
What you would do is at the top of every module that needs dots do:
local mydata = require(“mydata”)
then you would reference dots as:
mydata.dots[4].x = 100
Thanks for your time Rob… with your help I will get this to work
I must be doing something wrong, but here is the entire code
I have nothing else… a black screen with 10 circles, that’s it
I want to be able to move circle 4
in myData.lua
local M = { } M.level = 1 M.soundOn = true function createCircle (group) local dots = {} local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do dots[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert(dots[i]) dots[i].x = initialX + (separacion\*i) dots[i].y = initialY + distance\*1 dots[i]:scale(.5, .5) end end return M
and like you said… if I
print(myData.level)
it prints “1” in the console – so far so good
then in my storyboard, in my guitar.lua
local storyboard = require( "storyboard" ) local myData = require("myData") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view createCircle (group) --myData.dots[4].x = 100 end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event ) local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene() local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event ) local group = self.view end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
I have nothing there – only the function call – createCircle (group)
and it works, I see the 10 circles in a black screen…
when I type the code in comment, underneath the createCircle function
– myData.dots[4].x = 100
I get an ERROR
File: guitar.lua
Line: 11
Attempt to index field ‘dots’ (a nil value)
Something is wrong, I don’t know what…
I know you have tried in different ways, thanks you very much Rob for that…
but I guess I just need one final thing…
So you have the entire code there is nothing else in my 2 modules
Thanks for your time and your help… not only for me
but for all the people who are also reading at this
thank you
I would actually do this a little bit different. Bear with me. Name the file dots.lua, not myData.lua.
-- dots.lua local M = { } M.dot = {} -- table of individual dots. function dots.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dot[i].x = initialX + (separacion\*i) M.dot[i].y = initialY + distance\*1 M.dot[i]:scale(.5, .5) end end return M
then the top of your guitar.lua file:
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) dots.dot[4].x = 100 end
In this case we are taking the idea from the no more globals post, but instead of creating a “global” space to store things, you’re making a module specific to your “dots” code. If you later wanted a move function, you could do something like:
function M.moveDot( dotIndex, x, y )
M.dot[dotIndex].x = x
M.dot[dotIndex].y = y
end
in your dots.lua and then when you need to move a dot in guitar.lua simply do:
dots.moveDot(4, 100, 100)
Rob…I did what you said…
1-- change the name of the file, myData.lua for dots.lua
2-- I copy and paste the same code you gave me, for the dots.lua
-- dots.lua local M = { } M.dot = {} -- table of individual dots. function dots.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dot[i].x = initialX + (separacion\*i) M.dot[i].y = initialY + distance\*1 M.dot[i]:scale(.5, .5) end end return M
3-- then in the guitar.lua I have only this
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) dots.dot[4].x = 100 end
and I get same ERROR
File: dots.lua
Line: 6
Attempt to index global ‘dots’ (a nil value)
if I comment this line
–dots.dot[4].x = 100
I don’t even see the circles
I notice that, as far as I understand, the “Object” is this
M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60)
so to me the variable name is
M.dot[i]
now in my other module, in guitar.lua
I have this
dots.dot[4].x = 100
to me they are 2 different variables
one is dots.dot[4]
and the other is M.dot[i]
so I try to change the dots.dot for M.dot
and it did not work either…
we’re very close…
Sorry. Change this line:
function dots.createCircle (group)
to this line:
function M.createCircle (group)
Rob
Okay… I have in dots.lua
-- dots.lua local M = { } M.dot = {} -- table of individual dots. function M.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dot[i].x = initialX + (separacion\*i) M.dot[i].y = initialY + distance\*1 M.dot[i]:scale(.5, .5) end end return M
I changed the line…
in guitar.lua I have
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view M.createCircle (group) end
------------------- I get an ERROR
File: guitar.lua
Line: 10
Attempt to index global ‘M’ (a nil value)
---------*********-------------
If I do this
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view createCircle (group) end
I get an ERROR -----
File: guitar.lua
Line: 10
Attempt to call global ‘createCircle’ (a nil value)
there is something somewhere…
we’ll make it work
In guitar.lua change it back to dots.createCircle
It’s M.createCircle inside the module (M is for Module), but your refer to it as “dots” in your other modules.
Rob
I don’t want to give up…
and I don’t want to bother you so much
but it just doesn’t work…
– dots.lua
-- dots.lua local M = { } M.dot = {} -- table of individual dots. function M.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dot[i].x = initialX + (separacion\*i) M.dot[i].y = initialY + distance\*1 M.dot[i]:scale(.5, .5) end end return M
–guitar.lua
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) end
it seems to be really difficult to do this…but I know for sure we’ll make it work
I notice another thing…
in dots.lua I have – M.dot = { } ----- with no “s”
and in guitar.lua I have – dots.createCircle ----- with “s”
do that has something to do?
I change both ways and it doesn’t work any way
What error are you getting now?
this one
File: dots.lua
Line: 13
Attempt to index field ‘dots’ (a nil value)
and the only things I have is this
dots.lua
-- dots.lua local M = { } M.dot = {} -- table of individual dots. function M.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dot[i].x = initialX + (separacion\*i) M.dot[i].y = initialY + distance\*1 M.dot[i]:scale(.5, .5) end end return M
and guitar.lua
--guitar.lua local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) dots.dot[4].x = 100 end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event ) local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene() local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event ) local group = self.view end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
that’s it…
Look at line 13 of dots.lua. Do you see a reference to “dots”? Look at the rest of the function. Are you referencing “dots” or “dot”?
Rob
Okay…I changed that for this
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) dots.dots[4].x = 100 end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event ) local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene() local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event ) local group = self.view end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
Same ERROR
File: dots.lua
Line: 13
Attempt to index field ‘dots’ (a nil value)
Can you post your dots.lua?
–dots.lua
-- dots.lua local M = { } M.dot = {} -- table of individual dots. function M.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dot[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dot[i].x = initialX + (separacion\*i) M.dot[i].y = initialY + distance\*1 M.dot[i]:scale(.5, .5) end end return M
guitar.lua
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) dots.dots[4].x = 100 end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event ) local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene() local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event ) local group = self.view end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
this is all I have
Rob… Finally we got it…YES!!!
when I post this new one I notice the
M.dot = { }
I said , maybe here is the problem…
I change that for
M.dots = { }
and of course I change all the
dot for dots
and i works!!!
I/m posting here the final 2 files that DO WORK
for other people if they want to do the same
Thank you very much Rob for your help…
FINAL FILES
– dots.lua
-- dots.lua local M = { } M.dots = {} -- table of individual dots. function M.createCircle (group) local separacion = 40 local initialX = display.contentCenterX - 190 local initialY = display.contentCenterY local distance = 30 for i=1, 10 do M.dots[i] = display.newImageRect ("images/circleYes.png", 60, 60) group:insert( M.dots[i] ) M.dots[i].x = initialX + (separacion\*i) M.dots[i].y = initialY + distance\*1 M.dots[i]:scale(.5, .5) end end return M
guitar.lua
local storyboard = require( "storyboard" ) local dots = require("dots") local scene = storyboard.newScene() ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view dots.createCircle (group) dots.dots[4].y = 149 end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event ) local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene() local group = self.view end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event ) local group = self.view end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
I hope this helps someone else
Thanks Rob