how to insert a group in a require

I have 2 files

– main.lua

–extraStuff.lua

Normally I insert an object into a group.

when I go to another scene (in storyboard) the object is gone.

    line1 = display.newRect( 0, 0, 40, 5)     group:insert(line1) -- Right Here     line1.x = centerX     line1.y = centerY

this in main.lua


now, let’s say I have many objects and lines…

so I put all of those inside a function

– function myNewFunction (  )

           line1 = display.newRect( 0, 0, 40, 5)

           group:insert(line1) – In this file extraStuff.lua the “group” it doesn’t exist

           line1.x = centerX
           line1.y = centerY

end

and I have this new function placed in another file.lua – let’s say extraStuff.lua


now, in my createScene of my app – in main.lua – I have created a group

– local group = self.view

if I want to use all of the stuff I have in myNewFunction

I just “require” the .lua file (extraStuff.lua) in main.lua


–local extraStuff = require( “extraStuff” ) – so main.lua knows about it


and in my createScene, in main.lua

instead of typing all the lines and images and code of myNewFunction

I just call the function

– myNewFunction (  )

and PROBLEM –

is telling me that it can not find the “group”

because the “group” is created in main.lua, not in extraStuff.lua

and if I do it like this

function myNewFunction (  )            line1 = display.newRect( 0, 0, 40, 5)            line1.x = centerX            line1.y = centerY end

it works, but they are not inserted into a group


so how do I get ride of all the images in main.lua


I hope you can understand it, I try my best to explain the problem

thanks for all your help

How about changing the definition of myNewFunction to take a parameter:

function myNewFunction ( group )

    line1 = …

    group:insert(line1)

    …

end

then when you call it:

myNewFuction( group )

assuming group is the name of the group you want to use.

Rob

I did not really understand how that works, but it works perfect!

thank you Rob.

–still waiting for my app to be approved in apple.

Okay Rob, please help me with this:

I have a dots.lua file

function dotsFunction (group)     for i=1, 6 do         b1 = display.newImageRect ("images/noteCircle.png", 60, 60)         group:insert(b1)         b1.x = 100 + (40\*i)         b1.y = 100         b1:scale(.6, .6)     end end

so I create 6 images, circles – 1 to 6

in my guitar.lua file I require the dots.lua

and I have this function

    local function reChord ()         local thisIsRe = "D"              if thisIsRe == "D" then             b1.y = 50         print("Re")         end     end         dotsFunction(group)     reChord()

it works perfect, the last circle (number6) is Up and all five are aligned.

I have – b1.y = 50

but I want the circle number 3 to be 50, not the circle number 6

I have for i=1, 6

so I want to move only 3, or 2

how do I distinguish them

i think is inside the if thisIsRe == “D” then   but instead of b1.y – I can not put b3.y

so how do I do that?

thanks

You probably need to store them in a table/array so that you can reference each dot indepenendantly

Okay…would you help me understand it…

I use this Method for a table

 local colorTable = {         [1] = "blue",         [2] = "red",         [3] = "yellow",         [4] = "green",         [5] = "purple"     }     print( colorTable[3] )  -- output: "yellow"

so how would that work…

 local dotsFunction = {         [1] = "dot1",         [2] = "dot2",         [3] = "dot3",         [4] = "dot4",         [5] = "dot5" --  and so on until I get 40 dots...     }     print( dotsFunction[3] )  -- output: "yellow"

--------- then here

    local function reChord ()              if pmThisChordIs == "Em" then             dotsFunction[4].alpha = .5 -- something like that .... b1.alpha = .5         print("Re")         end     end         dotsFunction(group)     reChord()

but how do I create them?


    for i=1, #dotsFunction do         b1 = display.newImageRect ("images/noteCircle.png", 60, 60)         group:insert(b1)         b1.x = initialX + (separacion\*i)         b1.y = initialY + distance\*1         b1:scale(.5, .5)     end

as you can see I’m kind of confuse…

    local dots = {}       for i=1, #dotsFunction do         dots[i] = display.newImageRect ("images/noteCircle.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

or something like that.  Replace the references to b1 with dots[] and the index to the dot you’re interested in.

Rob

I create a function newDots (group) with the group

function newDots (group)     local dots = {}       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

and when I call the function it works perfect


    newDots(group)     dots[4].y = 50

but then I move the .y of the dots[4] and --ERROR

I think I’m very close Rob

thanks for your time and help…

I’d need to see more of your code and the whole stack trace

Rob

okay…

in dots.lua

function newDots (group)     local dots = {}      local separacion = 40      local initialX = display.contentCenterX - 190     local initialY = display.contentCenterY - 110     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

in guitar.lua

    newDots(group)

i require the dots.lua, and

I just call the function… it works perfect, I see 10 circles


now… How do write the code to make the circle no. 5 = alpha = .5

in other words, how do I acces each – dots[i] – individually?


dots[4].alpha=.5 – it doesn’t work

i hope this helps

I suspect the problem is that you have made dots local to the function newDots() which means you can’t access it outside of that function.  I would declare:

local dots = {}

at the top of your module and take the local off inside the function.

Rob

I try this

    local dots = {} function newDots (group)      local separacion = 40      local initialX = display.contentCenterX - 190     local initialY = display.contentCenterY - 110     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

the table dots = { } – is outside the newDots function


then on guitar.lua I do this


    newDots(group)     dots[4].alpha=.5

still it does not work!


File: guitar.lua
Line: 154
Attempt to index upvalue ‘dots’ (a boolean value)

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