index upvalue error

I’m calling pauseGameFunction (present in pauseGame.lua) from my mars.lua file

and it gives the following error:

However I did the same thing for my background function (present in background.lua) and it runs perfectly …

I don’t even understand the meaning of the error …

You’re going to need to show us some code around the error to have any way of helping.

Rob

------------------------------------------------------------------------------------------- mars.lua ------------------------------------------------------------------------------------------ local function pauseGame(event) pauseGame.pauseGameFunction(); gamePaused=true; end pauseBtn:addEventListener("touch",pauseGame) ------------------------------------------------------------------------------------------- pauseGame.lua ------------------------------------------------------------------------------------------ P={} function pauseGameFunction() pauseBg = display.newImageRect(PAUSE,"assets/images/pauseBg.png",\_W,\_H) pauseBg.alpha = 0.3 restartBtn = display.newImageRect(PAUSE,"assets/images/restartBtn.png",100,30) restartBtn.x=0 restartBtn.y=\_H-restartBtn.contentHeight\*2 function restartGame(event) if event.phase == "began" then restartBtn.xScale = 0.9; restartBtn.yScale = 0.9; elseif event.phase == "ended" then restartBtn.xScale = 1; restartBtn.yScale = 1; cleanMemory() display.remove(character) composer.gotoScene("pleaseWait") return true; end end restartBtn:addEventListener("touch",restartGame) resumeBtn = display.newImageRect(PAUSE,"assets/images/resumeBtn.png",100,30) resumeBtn.x=\_W-resumeBtn.contentWidth resumeBtn.y=\_H-resumeBtn.contentHeight\*2 function resumeGame(event) if event.phase == "began" then resumeBtn.xScale = 0.9; resumeBtn.yScale = 0.9; elseif event.phase == "ended" then resumeBtn.xScale = 1; resumeBtn.yScale = 1; flyBtn.isVisible = true; pauseBtn.isVisible = true; fireBtn.isVisible = true; resumeBtn.isVisible=false; restartBtn.isVisible=false; exitBtn.isVisible=false; pauseBg.isVisible=false; gamePaused=false; character.bodyType = "dynamic" -- timer.resume(timerCoin) -- timer.resume(timerRun ) -- timer.resume(timerMShip ) -- timer.resume(timerUFO) -- timer.resume(timerShield) Runtime:addEventListener("enterFrame", bg1) Runtime:addEventListener("enterFrame", bg2) Runtime:addEventListener("enterFrame", bg3) Runtime:addEventListener("enterFrame", bg4) Runtime:addEventListener("enterFrame", bg5) return true; end end resumeBtn:addEventListener("touch",resumeGame) menuBtn = display.newImageRect(PAUSE,"assets/images/menuBtn.png",100,30) menuBtn.x=0; menuBtn.y=\_H-menuBtn.contentHeight; function goBackToMenu(event) cleanMemory() display.remove(character) composer.gotoScene("menu") return true; end menuBtn:addEventListener("touch", goBackToMenu) exitBtn = display.newImageRect(PAUSE,"assets/images/exitBtn.png",100,30) exitBtn.x=\_W-exitBtn.contentWidth exitBtn.y=\_H-exitBtn.contentHeight function exitBtnFunc(event) if event.phase == "began" then exitBtn.xScale = 0.9; exitBtn.yScale = 0.9; elseif event.phase == "ended" then exitBtn.xScale = 1; exitBtn.yScale = 1; if platformName == "Android" then native.requestExit(); else os.exit() end return true; end end exitBtn:addEventListener("touch", exitBtnFunc) -- function onSystemEvent( event ) -- if ( event.type == "applicationSuspend" ) then -- pauseGame() -- end -- end end P.pauseGameFunction = pauseGameFunction return P

My motive behind putting all things in pauseGame.lua file is to reduce the loc in my mars.lua … and use different files for different functions

put a:   local pauseGame = require(“pauseGame”) at the top of mars.lua, however you cannot use “pauseGame” as the name of a function later in the code.  Change that function and you should be good.

Rob

thank u so much … I was unaware that i cannot use same function name later … 

Function names are just variable names that hold a pointer to a function.  If you later assign a number to it, the function will be lost and it will be a simple number variable.   In your case you’re creating a table/object:

local pauseGame = require(“pauseGame”)

then later when you do:

local function pauseGame(…)

it will overwrite the earlier reference and later on when you try to access things that you required in, you won’t find them.

Rob

thanks a lot …

and one more thing …

I just figured out that if I write a function in x.lua and call it in y.lua , it still works.

I mean I did not created any table like :

P = {}

P.pauseGameFunction = pauseGameFunction
return P

just wrote the function and local coins = require “coins” .

so whats the use of those 3 statements . I used it because I saw it in one of the forum topics.

Lets say you make y.lua and it has a function like:

function doSomething()

     …

end

That function becomes global and it’s available throughout your program.   When you do:

local P = {}

function P.someFunction()

    …

end

return P

someFunction is member of object P, not a global function.

BTW:  when you do P = {} without the local in front, P becomes a global object.  If you use P = {} in another module, it will over write your other P objects.  Make sure to put the local in front.

Rob

You’re going to need to show us some code around the error to have any way of helping.

Rob

------------------------------------------------------------------------------------------- mars.lua ------------------------------------------------------------------------------------------ local function pauseGame(event) pauseGame.pauseGameFunction(); gamePaused=true; end pauseBtn:addEventListener("touch",pauseGame) ------------------------------------------------------------------------------------------- pauseGame.lua ------------------------------------------------------------------------------------------ P={} function pauseGameFunction() pauseBg = display.newImageRect(PAUSE,"assets/images/pauseBg.png",\_W,\_H) pauseBg.alpha = 0.3 restartBtn = display.newImageRect(PAUSE,"assets/images/restartBtn.png",100,30) restartBtn.x=0 restartBtn.y=\_H-restartBtn.contentHeight\*2 function restartGame(event) if event.phase == "began" then restartBtn.xScale = 0.9; restartBtn.yScale = 0.9; elseif event.phase == "ended" then restartBtn.xScale = 1; restartBtn.yScale = 1; cleanMemory() display.remove(character) composer.gotoScene("pleaseWait") return true; end end restartBtn:addEventListener("touch",restartGame) resumeBtn = display.newImageRect(PAUSE,"assets/images/resumeBtn.png",100,30) resumeBtn.x=\_W-resumeBtn.contentWidth resumeBtn.y=\_H-resumeBtn.contentHeight\*2 function resumeGame(event) if event.phase == "began" then resumeBtn.xScale = 0.9; resumeBtn.yScale = 0.9; elseif event.phase == "ended" then resumeBtn.xScale = 1; resumeBtn.yScale = 1; flyBtn.isVisible = true; pauseBtn.isVisible = true; fireBtn.isVisible = true; resumeBtn.isVisible=false; restartBtn.isVisible=false; exitBtn.isVisible=false; pauseBg.isVisible=false; gamePaused=false; character.bodyType = "dynamic" -- timer.resume(timerCoin) -- timer.resume(timerRun ) -- timer.resume(timerMShip ) -- timer.resume(timerUFO) -- timer.resume(timerShield) Runtime:addEventListener("enterFrame", bg1) Runtime:addEventListener("enterFrame", bg2) Runtime:addEventListener("enterFrame", bg3) Runtime:addEventListener("enterFrame", bg4) Runtime:addEventListener("enterFrame", bg5) return true; end end resumeBtn:addEventListener("touch",resumeGame) menuBtn = display.newImageRect(PAUSE,"assets/images/menuBtn.png",100,30) menuBtn.x=0; menuBtn.y=\_H-menuBtn.contentHeight; function goBackToMenu(event) cleanMemory() display.remove(character) composer.gotoScene("menu") return true; end menuBtn:addEventListener("touch", goBackToMenu) exitBtn = display.newImageRect(PAUSE,"assets/images/exitBtn.png",100,30) exitBtn.x=\_W-exitBtn.contentWidth exitBtn.y=\_H-exitBtn.contentHeight function exitBtnFunc(event) if event.phase == "began" then exitBtn.xScale = 0.9; exitBtn.yScale = 0.9; elseif event.phase == "ended" then exitBtn.xScale = 1; exitBtn.yScale = 1; if platformName == "Android" then native.requestExit(); else os.exit() end return true; end end exitBtn:addEventListener("touch", exitBtnFunc) -- function onSystemEvent( event ) -- if ( event.type == "applicationSuspend" ) then -- pauseGame() -- end -- end end P.pauseGameFunction = pauseGameFunction return P

My motive behind putting all things in pauseGame.lua file is to reduce the loc in my mars.lua … and use different files for different functions

put a:   local pauseGame = require(“pauseGame”) at the top of mars.lua, however you cannot use “pauseGame” as the name of a function later in the code.  Change that function and you should be good.

Rob

thank u so much … I was unaware that i cannot use same function name later … 

Function names are just variable names that hold a pointer to a function.  If you later assign a number to it, the function will be lost and it will be a simple number variable.   In your case you’re creating a table/object:

local pauseGame = require(“pauseGame”)

then later when you do:

local function pauseGame(…)

it will overwrite the earlier reference and later on when you try to access things that you required in, you won’t find them.

Rob

thanks a lot …

and one more thing …

I just figured out that if I write a function in x.lua and call it in y.lua , it still works.

I mean I did not created any table like :

P = {}

P.pauseGameFunction = pauseGameFunction
return P

just wrote the function and local coins = require “coins” .

so whats the use of those 3 statements . I used it because I saw it in one of the forum topics.

Lets say you make y.lua and it has a function like:

function doSomething()

     …

end

That function becomes global and it’s available throughout your program.   When you do:

local P = {}

function P.someFunction()

    …

end

return P

someFunction is member of object P, not a global function.

BTW:  when you do P = {} without the local in front, P becomes a global object.  If you use P = {} in another module, it will over write your other P objects.  Make sure to put the local in front.

Rob