attempt to index local 'newAstroid' (a nil value)

Hi All

Working on the Chapter2 “upward and Downward” tutorial for Corona.  Searched the forums for something similar but did not find the answer.  When I try to relaunch my app I get:

main.lua:106 attempt to index local ‘newAsteroid’ (a nil value) stack traceback:
   main.lua in main chunk

main.lua:112 attempt to index local ‘newAsteroid’ (a nil value) stack traceback:
   main.lua in main chunk

main.lua:118 attempt to index local ‘newAsteroid’ (a nil value) stack traceback:
   main.lua in main chunk

if ( whereFrom == 1) then
– from left
   newAsteroid.x = -60 –line 106
   newAsteroid.y = math.random( 500 )    
   newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )
 
elseif ( whereFrom == 2 ) then   
– from top
   newAsteroid.x = math.random( display.contentWidth ) –line 112
   newAsteroid.y = -60    
   newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) )
   
elseif ( whereFrom == 3 ) then
– from right
   newAsteroid.x = display.contentWidth + 60 – line 118
   newAsteroid.y = math.random( 500 )
   newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )
 
 end

Mind posting the code when you defined newAsteroid?

The most common mistake is that newAsteroid is out of scope or you are trying to load an image it can’t find.

Hello Lua

Pardon my noobishness.  This is it…correct?

way up top I have

local newAsteroid

And the further down I have

local function createAsteroid()
   newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )
   table.insert( asteroidsTable, newAsteroid )
   physics.addBody ( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } )
   newAsteroid.myName = “asteroid”
end

No need to apologize. These are good new user questions. You’re entering into wonderful understanding of programming called “Scope”. Just like a telescope lets you see things (or a microscope), scope basically comes down to is something visible.

Lets say you have:

local function createAsteroid()      local newAsteroid = display.newImageRect(....) end

Then you have another function:

local function destroyAsteroid()      display.remove( newAsteroid )      newAsteroid = nil end

This would not work. When you first declared newAsteroid and made it a display object it was “local” to the function createAsteroid(). No other part of your app can see the newAsteroid object.

You could leave the “local” off at which point newAsteroid becomes a global variable and as someone learning, globals are bad with limited exceptions where experienced developers need to go. While it would work, it’s bad.

Putting:

local newAsteriod

by itself near the top of the modules means that any block of code later on will know about newAsteroid. So this is a very important technique if you need to create something in one function and manipulate it in a different function.

Because you are also creating a table of asteroids (“asteroidsTable”), you’re going to use that to keep track of your different asteroids and it will keep a reference to each asteroid for you. So you do not need to pre-declare newAsteroid at the top because when you go to delete the asteroid, you will be using the asteroidsTable array to remove it.

The forward declared variable a limited resource. Lua caps it out at 200 per module. Two hundred seems like a lot, but you will run out faster than you think. I’m building a game and in the game module alone, I’m already at 50. I have over 240 local’s declared in all the modules. You will learn how to make modules in a couple of chapters, for now, just keep in mind that it’s a limited resource.

Rob

Hi Rob

Not sure what all that means.  You would think I would by now with the Java I have been studying the past couple weeks and a little bit of Python before that.

After I removed “local newAsteriod” from the code, I get the same exact errors except this time it says “global” instead of “local”  I was under the impression that using the word local was just declaring a variable?

From chapt 1 of the tute:

“The first word, local, is a Lua command indicating that the next word will be a variable. A variable, just like you learned in math class, is used to store a value.”

I believe the error is due to how you are creating the image.

display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

try this instead to check if it is

display.newRect(mainGroup,0,0,102,85)

Hi Lua

no, Sorry.  Same Global errors with this code:

local function createAsteroid()
   newAsteroid = display.newRect(mainGroup,0,0,102,85)
   table.insert( asteroidsTable, newAsteroid )
   physics.addBody ( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } )
   newAsteroid.myName = “asteroid”
end

Are you even calling createAsteroid before you referer to newasteroid?

Aren’t I in the code I posted?

No where in this thread you called createAsteroid :confused:

ctrl + f “createAsteroid” and all you will find is you defining the function but not calling it

not here?  local function createAsteroid()

again…total noob here.  be gentle LOL

Thats defining a function

to call a function you do
createAsteroid()

Hmmmmm.  I don’t see that bit of code until LATER in the tutorial.   Maybe I tested the app to soon?  Here is what you are rferring to at the end of the chapter:

Add the following function to your main.lua file following the code you’ve already added:

local function gameLoop()

– Create new asteroid
createAsteroid()

– Remove asteroids which have drifted off screen
for i = #asteroidsTable, 1, -1 do
local thisAsteroid = asteroidsTable[i]

if ( thisAsteroid.x < -100 or
thisAsteroid.x > display.contentWidth + 100 or
thisAsteroid.y < -100 or
thisAsteroid.y > display.contentHeight + 100 )
then
display.remove( thisAsteroid )
table.remove( asteroidsTable, i )
end
end
end

yes.

local function createAsteroid() local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 ) table.insert( asteroidsTable, newAsteroid ) physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) newAsteroid.myName = "asteroid" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newAsteroid.x = -60 newAsteroid.y = math.random( 500 ) newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newAsteroid.x = math.random( display.contentWidth ) newAsteroid.y = -60 newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newAsteroid.x = display.contentWidth + 60 newAsteroid.y = math.random( 500 ) newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end newAsteroid:applyTorque( math.random( -6,6 ) ) end

In this tutorial, newAsteroid is not used in any other function.

Hi sdktester15

You code tweak helped clear up all error messages.  Should I be seeing an Asteroid though?  :slight_smile:

timer.performWithDelay(1000,function()
createAsteroid()
end,0)

this may not be what the tutoral is telling you to do but this will spawn an asteroid every 1 second

@beyond2k, the getting started guide has code that needs to be complete before it can run. This is a great example. Functions alone don’t do anything until you call them. Go ahead and finish the chapter and see where you are.

Rob

Hi Guys.  Thanks

Thanks, Rob!  I will try today.  :slight_smile: