Including and initializing my own Lib

Im a total novice and I wanted to make a game.lua file that uses a require “marbles.lua” which contains functions I would need to spawn marbles (from top, falling to bottom).  I’m not entire sure how to do this, but this is what I ended up doing. Any advice would help.

game.lua:

--------------------------------------------------------------------------------- -- -- scene.lua -- --------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) local physics = require("physics") local marbles = require("marbles") local marbleHandler -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local bkg = display.newImage("images/Default-568h@2x.png", display.contentWidth\*0.5, display.contentHeight\*0.5 ) marbleHandler = marbles.new() end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then physics.start() marbleHandler.addNewMarble() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Now marble.lua
 

local M = {} function M.new(listener, options) -- make a new three match board options = options or {} local difficulty = options.difficulty or 1 local x,y = options.x or math.random(display.contentWidth\*0.1, display.contentHeight\*0.9), options.y or -200 local w,h = options.w or 70, options.h or 70 local function marbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = score + 1 --scoreText.text = score end end local function blackMarbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = math.floor(score \* 0.5) --scoreText.text = score end end local function offscreen(self, event) if (self.y == nil) then return end if (self.y \> display.contentHeight + 50) then Runtime:removeEventListener("enterFrame", self) self:removeSelf() end end function addNewMarble() if(math.random(1,5)==1) then --Black Marble local blackMarble = display.newImage( "images/5.png", x, y) blackMarble.width = w; blackMarble.height = h; physics.addBody(blackMarble) blackMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", blackMarble) blackMarble:addEventListener("touch", marbleTouched) else --Normal Marble local redMarble = display.newImage("images/4.png", x, y) redMarble.width = w; redMarble.height = h; physics.addBody(redMarble) redMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", redMarble) redMarble:addEventListener("touch", marbleTouched) end end end return M

I’ve run into a few errors, and I couldn’t find tutorials guidelines on how to accomplish this, but here’s the error I’m getting:

 

13:01:30.106 ERROR: Runtime error 13:01:30.106 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:45: attempt to index upvalue 'marbleHandler' (a nil value) 13:01:30.106 stack traceback: 13:01:30.106 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:45: in function \<C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:32\> 13:01:30.106 ?: in function 'dispatchEvent' 13:01:30.106 ?: in function \<?:865\> 13:01:30.106 (tail call): ? 13:01:30.106 ?: in function \<?:504\> 13:01:30.106 ?: in function \<?:169\>

The error on line 45 of game.lua is because you’re not returning anything from your new() function in marbles.lua

What you’ve effectively done in marbles.lua with the addNewMarble function is to declare a global function. You haven’t associated it with anything.

Here’s my version of your code, with comments in CAPITALS to explain the changes… (warning: I have not run this so I can’t guarantee anything)…

game.lua

local sceneName = "game" -- NOT SURE WHAT ... WAS DOING local composer = require( "composer" ) local physics = require("physics") local marbles = require("marbles") physics.start() -- MOVED HERE TO AVOID STARTING REPEATEDLY local marbleHandler -- Load scene with same root filename as this file -- ??? (Maybe I just learnt something) local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local bkg = display.newImage("images/Default-568h@2x.png", display.contentWidth\*0.5, display.contentHeight\*0.5 ) marbleHandler = marbles.new() end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then -- physics.start() -- REMOVED TO AVOID REPEATED CALLS marbleHandler.addNewMarble() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

marbles.lua

local M = {} function M.new(listener, options) -- make a new three match board local options = options or {} -- MADE LOCAL TO AVOID BEING GLOBAL local difficulty = options.difficulty or 1 local x,y = options.x or math.random(display.contentWidth\*0.1, display.contentHeight\*0.9), options.y or -200 local w,h = options.w or 70, options.h or 70 local function marbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = score + 1 --scoreText.text = score end end local function blackMarbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = math.floor(score \* 0.5) --scoreText.text = score end end local function offscreen(self, event) if (self.y == nil) then return end if (self.y \> display.contentHeight + 50) then Runtime:removeEventListener("enterFrame", self) self:removeSelf() end end local function addNewMarble() if(math.random(1,5)==1) then --Black Marble local blackMarble = display.newImage( "images/5.png", x, y) blackMarble.width = w; blackMarble.height = h; physics.addBody(blackMarble) blackMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", blackMarble) blackMarble:addEventListener("touch", marbleTouched) return blackMarble -- RETURN THE NEW BLACK MARBLE else --Normal Marble local redMarble = display.newImage("images/4.png", x, y) redMarble.width = w; redMarble.height = h; physics.addBody(redMarble) redMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", redMarble) redMarble:addEventListener("touch", marbleTouched) return redMarble -- RETURN THE NEW RED MARBLE end end return addNewMarble() -- RETURN THE NEW INSTANCE OF THE MARBLE OBJECT end return M -- THIS IS JUST RETURNING A SINGLETON REFERENCE TO THE MARBLE LIBRARY

Thanks for the quick response, but now it shows this:

 

18:29:27.823 ERROR: Runtime error 18:29:27.823 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:37: attempt to call field 'addNewMarble' (a nil value) 18:29:27.823 stack traceback: 18:29:27.823 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:37: in function \<C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:28\> 18:29:27.823 ?: in function 'dispatchEvent' 18:29:27.823 ?: in function \<?:865\> 18:29:27.823 (tail call): ? 18:29:27.823 ?: in function \<?:504\> 18:29:27.823 ?: in function \<?:169\>

I apologize for the noobness.

 

The error on line 45 of game.lua is because you’re not returning anything from your new() function in marbles.lua

What you’ve effectively done in marbles.lua with the addNewMarble function is to declare a global function. You haven’t associated it with anything.

Here’s my version of your code, with comments in CAPITALS to explain the changes… (warning: I have not run this so I can’t guarantee anything)…

game.lua

local sceneName = "game" -- NOT SURE WHAT ... WAS DOING local composer = require( "composer" ) local physics = require("physics") local marbles = require("marbles") physics.start() -- MOVED HERE TO AVOID STARTING REPEATEDLY local marbleHandler -- Load scene with same root filename as this file -- ??? (Maybe I just learnt something) local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local bkg = display.newImage("images/Default-568h@2x.png", display.contentWidth\*0.5, display.contentHeight\*0.5 ) marbleHandler = marbles.new() end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then -- physics.start() -- REMOVED TO AVOID REPEATED CALLS marbleHandler.addNewMarble() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

marbles.lua

local M = {} function M.new(listener, options) -- make a new three match board local options = options or {} -- MADE LOCAL TO AVOID BEING GLOBAL local difficulty = options.difficulty or 1 local x,y = options.x or math.random(display.contentWidth\*0.1, display.contentHeight\*0.9), options.y or -200 local w,h = options.w or 70, options.h or 70 local function marbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = score + 1 --scoreText.text = score end end local function blackMarbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = math.floor(score \* 0.5) --scoreText.text = score end end local function offscreen(self, event) if (self.y == nil) then return end if (self.y \> display.contentHeight + 50) then Runtime:removeEventListener("enterFrame", self) self:removeSelf() end end local function addNewMarble() if(math.random(1,5)==1) then --Black Marble local blackMarble = display.newImage( "images/5.png", x, y) blackMarble.width = w; blackMarble.height = h; physics.addBody(blackMarble) blackMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", blackMarble) blackMarble:addEventListener("touch", marbleTouched) return blackMarble -- RETURN THE NEW BLACK MARBLE else --Normal Marble local redMarble = display.newImage("images/4.png", x, y) redMarble.width = w; redMarble.height = h; physics.addBody(redMarble) redMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", redMarble) redMarble:addEventListener("touch", marbleTouched) return redMarble -- RETURN THE NEW RED MARBLE end end return addNewMarble() -- RETURN THE NEW INSTANCE OF THE MARBLE OBJECT end return M -- THIS IS JUST RETURNING A SINGLETON REFERENCE TO THE MARBLE LIBRARY

Thanks for the quick response, but now it shows this:

 

18:29:27.823 ERROR: Runtime error 18:29:27.823 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:37: attempt to call field 'addNewMarble' (a nil value) 18:29:27.823 stack traceback: 18:29:27.823 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:37: in function \<C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:28\> 18:29:27.823 ?: in function 'dispatchEvent' 18:29:27.823 ?: in function \<?:865\> 18:29:27.823 (tail call): ? 18:29:27.823 ?: in function \<?:504\> 18:29:27.823 ?: in function \<?:169\>

I apologize for the noobness.