Attempt to index upvalue " " (a nil value)

Hello,

I’m currently just playing around with Corona SDK, seeing what I can make and what-not, and came across this problem. On line 54 I get the following error: “attempt to index upvalue ‘turtle’ (a nil value).” This is probably a scope problem but I thought I successfully worked around it by placing a “local turtle” at the very top of my main.lua page?

How can I avoid this in the future?

Thanks,

Angus

-- Remove status bar display.setStatusBar(display.HiddenStatusBar) -- Setting default anchors display.setDefault("anchorX", 0) display.setDefault("anchorY", 0) local physics = require("physics") physics.start() local gameIsActive = true local floor local turtle local gameLayer = display.newGroup() ------------------- -- -- Game Loop ------------------- -- local function gameLoop(event) -- Set up background local background = display.newImage("tpbg.png") background.name = "background" gameLayer:insert(background) -- Set up floor floor = display.newImage("tpfloor.png") floor.y = display.contentHeight - floor.contentHeight floor.name = "floor" gameLayer:insert(floor) function spawnTurtle() -- print("time two") turtle = display.newImage("turtle.png") x = math.random(0, display.contentWidth - turtle.contentWidth) turtle.x = x turtle.y = 0 turtle.name = "turtle" physics.addBody(turtle, "dynamic") end local function checkForCollision(event) if(event.phase == "began") then print(self.myName .. ": collision began with " .. event.other.myName) elseif(event.phase == "ended") then print(self.myName .. ": collision ended with " .. event.other.myName) end end if(gameIsActive == true) then timer.performWithDelay(1000, spawnTurtle, 10) turtle.collision = checkForCollision turtle:addEventListener("collision", turtle) floor.collision = checkForCollision floor:addEventListener("collision", floor) -- print("it worked") end end gameLoop()

Yes, everything worked fine until I added the local “checkForCollision” function as well as the 4 lines of “collision” code starting on line 54.

Hey.

Sorry. I cant see the line numbers but i see your error. You are trying to add an event listener to a nil variable. At the time you add your event listener, your turtle variable is nil.

At that time your turtle has not been assigned to the display object

How would I go about fixing that? I’ve basically referenced the “turtle” variable at the very top of my code and unfortunately can’t see any other way of defining the image as I need it to spawn in the “spawnTurtle” function.

A quick fix is to Move the spawn function below the collision function, and move the lines that add the collision listener into the spawn function.

There are other problems with your code also… I suggest googling for a blog post on this site entitled: “spawning objects the right way”

It will give you a better sense of the problems your spawn function has, without me reinventing the wheel explaining it again :slight_smile:

Thank you very much!

Alright, so I took a look at the page you suggested and it helped quite a bit. As I am fairly new, It’s always nice coming across pages like that as they help me improve. I do have a question though. I followed all of the instructions, however, I am not sure how I would create a 1 second delay in between all of the turtles spawning. Since I created a spawnTable as well as a for function, they spawn all at once.

Yes, everything worked fine until I added the local “checkForCollision” function as well as the 4 lines of “collision” code starting on line 54.

Hey.

Sorry. I cant see the line numbers but i see your error. You are trying to add an event listener to a nil variable. At the time you add your event listener, your turtle variable is nil.

At that time your turtle has not been assigned to the display object

How would I go about fixing that? I’ve basically referenced the “turtle” variable at the very top of my code and unfortunately can’t see any other way of defining the image as I need it to spawn in the “spawnTurtle” function.

A quick fix is to Move the spawn function below the collision function, and move the lines that add the collision listener into the spawn function.

There are other problems with your code also… I suggest googling for a blog post on this site entitled: “spawning objects the right way”

It will give you a better sense of the problems your spawn function has, without me reinventing the wheel explaining it again :slight_smile:

Thank you very much!

Alright, so I took a look at the page you suggested and it helped quite a bit. As I am fairly new, It’s always nice coming across pages like that as they help me improve. I do have a question though. I followed all of the instructions, however, I am not sure how I would create a 1 second delay in between all of the turtles spawning. Since I created a spawnTable as well as a for function, they spawn all at once.