Help needed with 'attempt to index global'

Hi all,

I have an error occur in one of my classes for the game I am developing, am a relative newbie to corona so any advice and help would be greatly appreciated.

The error is found in my level select class - “attempt to index global ‘levelsUnlocked’”

local _M = {}

– Main function - MUST return a display.newGroup()
function _M.new()

– Display group object
local localGroup = display.newGroup()
– Screen dimming effect
local dimRect = effects.newDimRect()

local background = display.newImage( “mainmenu/levelComplete.png”, 0, 0)
localGroup:insert(background)

local arrow = display.newImage( “images/arrow.png”, 135, 400 )
localGroup:insert(arrow)

function arrow:touch (event)
if event.phase == ‘ended’ then
– Call effect and change scene
transition.to(dimRect, {time = effects.dimDelay, alpha = 1, onComplete = function ()
director:changeScene(‘level_select’)

templevelIncrement = levelIncrement + 1;
levelIncrement = templevelIncrement

levelsUnlocked[levelIncrement] = true

end})
end
end
arrow:addEventListener(‘touch’, arrow)
localGroup:insert(arrow)

– Insert and run dimming effect
localGroup:insert(dimRect)
transition.to(dimRect, {time = effects.dimDelay, alpha = 0})

– MUST return a display.newGroup()
return localGroup

end

return _M

As I said any help or ideas with what it could be would be appreciated, thank you :). Oh sorry about the messy code, I couldn’t get the html to copy into here!

Ross [import]uid: 106143 topic_id: 28236 reply_id: 328236[/import]

First tip - stick the code between code brackets, it formats your code and you don’t need to use html.

I suspect your problem is to do with:

levelsUnlocked[levelIncrement] = true

Where is levelsUnlocked defined? I suspect you have it defined in another class and so its scope is not available in this class.

Simple solution make it a global - OR:

a) pass it to this class.
b) store it as a variable, load it to a local variable in this class.

[import]uid: 33275 topic_id: 28236 reply_id: 114066[/import]

Hi SegaBoy,

Just realised I copied the wrong class into that first post! Oops! This is the correct class that the error refers too;

{
module(…, package.seeall)

local localGroup = display.newGroup()

_W = display.contentWidth/2
_H = display.contentHeight/2

function new ()
local LSbackground = display.newImage(“Images/LSbackground.png”)
LSbackground.x = _W
LSbackground.y = _H
localGroup:insert(LSbackground)

local LSLogo = display.newImage(“Images/LSLogo.png”)
LSLogo.x = _W
LSLogo.y = 40
localGroup:insert(LSLogo)
function changeLevel (event)
if event.phase == “ended” then
director:changeScene(“game1”, “crossfade”)
director:levelChange(event.target.levels)
end
end

print ( levelsUnlocked[1] );
print ( levelsUnlocked[2] );
print ( levelsUnlocked[3] );
print ( levelsUnlocked[4] );
print ( levelsUnlocked[5] );
print ( levelsUnlocked[6] );
print ( levelsUnlocked[7] );
print ( levelsUnlocked[8] );
print ( levelsUnlocked[9] );
print ( levelsUnlocked[10] );
print ( levelsUnlocked[11] );
print ( levelsUnlocked[12] );

if (levelsUnlocked[1])
then
local levelOne = display.newImage(“Images/button1.png”)
levelOne.x = 15
levelOne.y = 60
levelOne.levels = 1
levelOne.xScale = 0.4
levelOne.yScale = 0.4

localGroup:insert(levelOne)
levelOne:addEventListener(“touch”, changeLevel)
end

if (levelsUnlocked[2])
then
local levelTwo = display.newImage(“Images/button2”)
levelTwo.x = 25
levelTwo.y = 60
levelTwo.levels = 2
levelTwo.xScale = 0.4
levelTwo.yScale = 0.4

localGroup:insert(levelTwo)
levelTwo:addEventListener(“touch”, changeLevel)
end

--------------------------------Level Three Button
if (levelsUnlocked[3])
then
local levelThree = display.newImage(“Images/button3”, 135, 60)
levelThree.levels = 3
levelThree.xScale = 0.4
levelThree.yScale = 0.4
levelThree:setReferencePoint(display.CentreReferncePoint);
localGroup:insert(levelThree)
levelThree:addEventListener(“touch”, changeLevel );

end
–levelThree.scene = “level3”

--------------------------------Level Four Button
if (levelsUnlocked[4])
then
local levelFour = display.newImage(“Images/button4”, 195, 60)
levelFour.levels = 4
levelFour.xScale = 0.4
levelFour.yScale = 0.4
levelFour:setReferencePoint(display.CentreReferncePoint);
localGroup:insert(levelFour)
levelFour:addEventListener(“touch”, changeLevel );
end
–levelFour.scene = “level4”

--------------------------------Level Five Button
if (levelsUnlocked[5])
then
local levelFive = display.newImage(“Images/button5”, 15, 120)
levelFive.levels = 5
levelFive.xScale = 0.4
levelFive.yScale = 0.4
levelFive:setReferencePoint(display.CentreReferncePoint);
localGroup:insert(levelFive)
levelFive:addEventListener(“touch”, changeLevel );
end
–levelFive.scene = “level5”

--------------------------------Level Six Button
if (levelsUnlocked[6])
then
local levelSix = display.newImage(“Images/button6”, 75, 120)
levelSix.levels = 6
levelSix.xScale = 0.4
levelSix.yScale = 0.4
levelSix:setReferencePoint(display.CentreReferncePoint);
localGroup:insert(levelSix)
levelSix:addEventListener(“touch”, changeLevel );
end
–levelSix.scene = “level6”

--------------------------------Level Seven Button
if (levelsUnlocked[7])
then
local levelSeven = display.newImage(“Images/button7”, 135, 120)
levelSeven.levels = 7
levelSeven.xScale = 0.4
levelSeven.yScale = 0.4
levelSeven:setReferencePoint(display.CentreReferncePoint);
localGroup:insert(levelSeven)
levelSeven:addEventListener(“touch”, changeLevel );
end
–levelSeven.scene = “level7”

--------------------------------Level Eight Button
if (levelsUnlocked[8])
then
local levelEight = display.newImage(“Images/button8”, 195, 120)
levelEight.levels = 8
levelEight.xScale = 0.4
levelEight.yScale = 0.4
levelEight:setReferencePoint(display.CentreReferncePoint);

localGroup:insert(levelEight)
levelEight:addEventListener(“touch”, changeLevel );
end
–levelEight.scene = “level8”

--------------------------------Level Nine Button
if (levelsUnlocked[9])
then
local levelNine = display.newImage(“Images/button9”, 15, 180)
levelNine.levels = 9
levelNine.xScale = 0.4
levelNine.yScale = 0.4
levelNine:setReferencePoint(display.CentreReferncePoint);

localGroup:insert(levelNine)
levelNine:addEventListener(“touch”, changeLevel );
end
–levelEight.scene = “level8”

--------------------------------Level Ten Button
if (levelsUnlocked[10])
then
local levelTen = display.newImage(“Images/button10”, 75, 180)
levelTen.levels = 10
levelTen.xScale = 0.4
levelTen.yScale = 0.4
levelTen:setReferencePoint(display.CentreReferncePoint);

localGroup:insert(levelTen)
levelTen:addEventListener(“touch”, changeLevel );
end
–levelEight.scene = “level8”

--------------------------------Level Eleven Button
if (levelsUnlocked[11])
then
local levelEleven = display.newImage(“Images/button11”, 135, 180)
levelEleven.levels = 8
levelEleven.xScale = 0.4
levelEleven.yScale = 0.4
levelEleven:setReferencePoint(display.CentreReferncePoint);

localGroup:insert(levelEleven)
levelEleven:addEventListener(“touch”, changeLevel );
end
–levelEight.scene = “level8”

--------------------------------Level Twelve Button
if (levelsUnlocked[12])
then
local levelTwelve = display.newImage(“Images/button12”, 195, 180)
levelTwelve.levels = 8
levelTwelve.xScale = 0.4
levelTwelve.yScale = 0.4
levelTwelve:setReferencePoint(display.CentreReferncePoint);

localGroup:insert(levelTwelve)
levelTwelve:addEventListener(“touch”, changeLevel );
end

return localGroup
end

function clean()
print (‘Clean function called from scene with module(…, package.seeall) !’)
end
}

Sorry about the confusion, staring at this code trying to find a reason is starting to play with my mind haha

Ross [import]uid: 106143 topic_id: 28236 reply_id: 114075[/import]

Same principle I can’t seem to find where the levelsUnlocked table/array is defined in that class.

P.S it would help if you stick the code between < code > tags however :slight_smile: [import]uid: 33275 topic_id: 28236 reply_id: 114076[/import]

Also for your own sanity it looks as though you’re mixing methods of using modules.

In your first you use the correct way of declaring an open table _M = {} and associating functions, variables, etc… with that. But in your new post you’re using the deprecated module(…, package.seeall)

You want to avoid the latter and certainly don’t use both. [import]uid: 33275 topic_id: 28236 reply_id: 114077[/import]

Okay then thank you, Il have a look at that and see if I can sort that out!

Haha in the future I will properly quote my code!

_M = {} definitley the best option to use then? [import]uid: 106143 topic_id: 28236 reply_id: 114083[/import]

Yep you’re essentially creating a table that stores all of the public functions you want to access, which is then returned to the variable you call with local foo = require(“foo”) - just make sure you return _M at the bottom of that class/module. [import]uid: 33275 topic_id: 28236 reply_id: 114089[/import]