ERROR: table expected...

ship = display.newImageRect( mainGroup, objectSheet, 4, 98, 79 )

ship.x = display.contentCenterX

ship.y = display.contentHeight - 100

physics.addBody( ship, { radius=30 } )-- line 73

ship.myName = “ship”

– Display lives and score

livesText = display.newText( uiGroup, "Lives: " … lives, 200, 80, native.systemFont, 36 )

scoreText = display.newText( uiGroup, "Score: " … score, 400, 80, native.systemFont, 36 )

local function updateText()

    livesText.text = "Lives: " … lives

    scoreText.text = "Score: " … score

end

local function createAsteroid()

   local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

 end 

 table.insert( asteroidsTable, newAsteroid )

 physics.addBody( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } ) --???  LINE 92

 newAsteroid.myName = “asteroid” 

I get this error: main.lua:92: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’.

I just started with corona and i got absolutely confused, because to me line 73 looks the same as 92, but i get an error on it, did everything by tutorial.

any help?

Since you have newAsteroid as “local” inside a function, it only exists in that function. This is called “scope” and we have a couple of tutorials on scope that will help you understand.

The fix is easy. Somewhere near the top of the module, do:

local newAsteroid

And inside the function take the “local” off. But that may not be the full fix. I don’t see where you ever call the function “createAsteroid” to run the code inside it. You could remove the function definition and it’s end statement as well or call

createAsteroid()

before you try and use it.

Rob

Thank you Bob, i managed to fix this issue, you’re a real miracle  ;) I’m sure we’ll meet somewhere here again, cheers.

Hi All

I have the same exact issue.  The code vytautas.meizenis presented is from the tutorial Chapt. 2 “Upward and onward”.  I am unclear on the resolution…  So there is a problem here:

local function createAsteroid()
local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )
end

Just tried this and the code still fails:

local ship
local gameLoopTimer
local livesText
local scoreText
local newAsteroid

local function createAsteroid()
newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )
end

Hello @beyond2k. I would love to help you, but I need more information.

First the code in the tutorial should run as is. Did you hand type it, or are you using the downloadable version?

Next, are you getting an error message? Error messages will tell you the line number where the problem is (though sometimes the line before it is really causing the the issue. Corona SDK should be opening up an console log window (white window behind the simulator). This is where output from the game: print statements, error and warning messages, etc. show up. It would be great if you could do a copy and paste of that message from the console log window to your forum response.

Some times here are no errors but things don’t work as expected. We need to know more than “it still fails”. What is failing? What are you expecting? What are you seeing? For instance, if you said “I run the program, but no asteroids are being created but I can move the ship and shoot.” then that gives us a starting point to try and help.

We also need to see the code around the error message or around where you think the problem is. Sometimes we will ask for more code, but dumping the whole project is going to overwhelm most viewers.  The more concise you can be the better. Also when posting code, you should always do a copy/paste as well. If you hand type the code into the forums, sometimes typos get in the way and we are not seeing the actual code. Finally always use code formatting when pasting code into the forums. That’s the blue <> button in the row with Bold, Italic, etc. Click the button and paste your code into the popup window.

Thanks

Rob

I went to chapter 3 to see what’s going on and this code:

&nbsp;table.insert( asteroidsTable, newAsteroid ) &nbsp;physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) --??? &nbsp;LINE 92 &nbsp;newAsteroid.myName = "asteroid"

needs to be inside the createAsteroid() function:

local function createAsteroid() &nbsp; &nbsp; local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 ) &nbsp;&nbsp;&nbsp; table.insert( asteroidsTable, newAsteroid ) &nbsp;&nbsp;&nbsp; physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) --??? &nbsp;LINE 92 &nbsp;&nbsp;&nbsp; newAsteroid.myName = "asteroid" end

Try that!

Rob

Hi Rob!

I tweaked the code and that error message went away.  However, I was expecting to see at lease on Asteroid at this point?

The rest of the tutorial will put the asteroid off screen and then set it in motion with:

newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

But if you have not set the .x and .y yet, I would think it would be partially on screen since it spawns it centered at 0, 0. A few lines down after it decides where the asteroid comes from (top left, top center, top right) and its set in motion with a series of the setLinearVelocity commands. Look in the top left corner and see if you see the bottom-right quarter of the asteroid.

Rob

Since you have newAsteroid as “local” inside a function, it only exists in that function. This is called “scope” and we have a couple of tutorials on scope that will help you understand.

The fix is easy. Somewhere near the top of the module, do:

local newAsteroid

And inside the function take the “local” off. But that may not be the full fix. I don’t see where you ever call the function “createAsteroid” to run the code inside it. You could remove the function definition and it’s end statement as well or call

createAsteroid()

before you try and use it.

Rob

Thank you Bob, i managed to fix this issue, you’re a real miracle  ;) I’m sure we’ll meet somewhere here again, cheers.

Hi All

I have the same exact issue.  The code vytautas.meizenis presented is from the tutorial Chapt. 2 “Upward and onward”.  I am unclear on the resolution…  So there is a problem here:

local function createAsteroid()
local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )
end

Just tried this and the code still fails:

local ship
local gameLoopTimer
local livesText
local scoreText
local newAsteroid

local function createAsteroid()
newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )
end

Hello @beyond2k. I would love to help you, but I need more information.

First the code in the tutorial should run as is. Did you hand type it, or are you using the downloadable version?

Next, are you getting an error message? Error messages will tell you the line number where the problem is (though sometimes the line before it is really causing the the issue. Corona SDK should be opening up an console log window (white window behind the simulator). This is where output from the game: print statements, error and warning messages, etc. show up. It would be great if you could do a copy and paste of that message from the console log window to your forum response.

Some times here are no errors but things don’t work as expected. We need to know more than “it still fails”. What is failing? What are you expecting? What are you seeing? For instance, if you said “I run the program, but no asteroids are being created but I can move the ship and shoot.” then that gives us a starting point to try and help.

We also need to see the code around the error message or around where you think the problem is. Sometimes we will ask for more code, but dumping the whole project is going to overwhelm most viewers.  The more concise you can be the better. Also when posting code, you should always do a copy/paste as well. If you hand type the code into the forums, sometimes typos get in the way and we are not seeing the actual code. Finally always use code formatting when pasting code into the forums. That’s the blue <> button in the row with Bold, Italic, etc. Click the button and paste your code into the popup window.

Thanks

Rob

I went to chapter 3 to see what’s going on and this code:

&nbsp;table.insert( asteroidsTable, newAsteroid ) &nbsp;physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) --??? &nbsp;LINE 92 &nbsp;newAsteroid.myName = "asteroid"

needs to be inside the createAsteroid() function:

local function createAsteroid() &nbsp; &nbsp; local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 ) &nbsp;&nbsp;&nbsp; table.insert( asteroidsTable, newAsteroid ) &nbsp;&nbsp;&nbsp; physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) --??? &nbsp;LINE 92 &nbsp;&nbsp;&nbsp; newAsteroid.myName = "asteroid" end

Try that!

Rob

Hi Rob!

I tweaked the code and that error message went away.  However, I was expecting to see at lease on Asteroid at this point?

The rest of the tutorial will put the asteroid off screen and then set it in motion with:

newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

But if you have not set the .x and .y yet, I would think it would be partially on screen since it spawns it centered at 0, 0. A few lines down after it decides where the asteroid comes from (top left, top center, top right) and its set in motion with a series of the setLinearVelocity commands. Look in the top left corner and see if you see the bottom-right quarter of the asteroid.

Rob