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