Code Runs on PC Simulator but not on Mac?

I’m having an issue with my code running on the PC simulator with no problems, but when I take it to mac it spits out an error:

Attempt to index global ‘object’ (a nil value)

At first I thought it might be the spelling on the filename… but I made sure cases match correctly.

Anything else that could cause code not to run on mac?

Hi @guillopuyol,

Can you please track down the line (in code) where this is happening, and show that code here?

Thanks,

Brent

The error shows it breaks on line 245…

 

--Function to spawn an object local function spawn(params) local isSprite = params.isSprite or 0 if isSprite== 1 then --creaye a physics object from sprite set object = display.newSprite(params.spriteSheet, params.spriteSequence) else --print("Running on Game:spawn") object = display.newImage(params.image) end --Set the objects table to a table passed in by parameters object.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index object.index = #object.objTable + 1 --Give the object a custom name object.myName = params.objectNamePrefix or "Object" --If the object should have a body create it, else dont. if params.hasBody then --Allow physics parameters to be passed by parameters: object.density = params.density or 0 object.friction = params.friction or 0 object.bounce = params.bounce or 0 object.isSensor = params.isSensor or false object.bodyType = params.bodyType or "dynamic" object.x=params.x or 0 object.y=params.y or 0 physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor}) object:setLinearVelocity( params.linearVelocityX, 0 ) end --Add Value if is coin if params.objectNamePrefix == "Coin" then object.coinValue=params.thisCoinValue end --The objects group object.group = params.group or nil --If the function call has a parameter named group then insert it into the specified group object.group:insert(object) --Insert the object into the table at the specified index object.objTable[object.index] = object return object end

Here is the code that calls the function…

local function spawnFuselagePowerUp(event) if nextFuselagePowerUp==0 then local spawns = spawn( { objectNamePrefix ="FuselagePowerUp", image = "Images/FuselagePowerUp.png", objTable = FuselagePowerUpSpawnTable, hasBody = true, density=.01, friction = 0.4, bounce = 0, isSensor=true, bodyType = "kinematic", group = localGroup, x=600, y=math.random(heli.y-200, heli.y+200), linearVelocityX=-200 } ) nextFuselagePowerUp=storyboard.gameVars.FuselagePowerUpFrequency --Now remove FuselagePowerUps out of the screen --for i = #spawnTable, 1, -1 do -- if spawnTable[i] ~= nil then -- if spawnTable[i].x \<-300 then -- display.remove(spawnTable[i]) -- table.remove(spawnTable,i) -- end -- end --end else if #FuselagePowerUpSpawnTable\>0 then if FuselagePowerUpSpawnTable[1].x \<-100 then --print("Total FuselagePowerUps Before: "..#FuselagePowerUpSpawnTable) display.remove(FuselagePowerUpSpawnTable[1]) table.remove(FuselagePowerUpSpawnTable,1) --print("Total FuselagePowerUps After: "..#FuselagePowerUpSpawnTable) end end nextFuselagePowerUp=nextFuselagePowerUp-1 end --myText.text = "FuselagePowerUps: "..#spawnTable end

oh yeah, and the forward declarations (I should have posted this on top… )

--Create a table to hold our spawns local FuselagePowerUpSpawnTable={} --Vars local nextFuselagePowerUp=0

Hi @guillopuyol,

I don’t see anything immediately “incorrect” except that “object” isn’t defined in the scope of the spawn function (and this shouldn’t be causing any issue between Mac and PC).

If it only happens when you try to display a sprite object, then I suspect there’s something wrong with how/when you set up the image sheet for the sprite… i.e., it can’t find the image sheet because of a scope issue, so it just fails to create the sprite and then errors out on line 245.

I’d suggest adding some “print()” lines in there and checking the value of each aspect as the function progresses up to line 245.

Sorry I couldn’t help more…

Brent

Thanks for the reply! I’m just confused on why it runs fine on PC but not on the Mac. If it was a scope issue wouldn’t it manifest itself regardless of the OS?

Hi @guillopuyol,

Can you please track down the line (in code) where this is happening, and show that code here?

Thanks,

Brent

The error shows it breaks on line 245…

 

--Function to spawn an object local function spawn(params) local isSprite = params.isSprite or 0 if isSprite== 1 then --creaye a physics object from sprite set object = display.newSprite(params.spriteSheet, params.spriteSequence) else --print("Running on Game:spawn") object = display.newImage(params.image) end --Set the objects table to a table passed in by parameters object.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index object.index = #object.objTable + 1 --Give the object a custom name object.myName = params.objectNamePrefix or "Object" --If the object should have a body create it, else dont. if params.hasBody then --Allow physics parameters to be passed by parameters: object.density = params.density or 0 object.friction = params.friction or 0 object.bounce = params.bounce or 0 object.isSensor = params.isSensor or false object.bodyType = params.bodyType or "dynamic" object.x=params.x or 0 object.y=params.y or 0 physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor}) object:setLinearVelocity( params.linearVelocityX, 0 ) end --Add Value if is coin if params.objectNamePrefix == "Coin" then object.coinValue=params.thisCoinValue end --The objects group object.group = params.group or nil --If the function call has a parameter named group then insert it into the specified group object.group:insert(object) --Insert the object into the table at the specified index object.objTable[object.index] = object return object end

Here is the code that calls the function…

local function spawnFuselagePowerUp(event) if nextFuselagePowerUp==0 then local spawns = spawn( { objectNamePrefix ="FuselagePowerUp", image = "Images/FuselagePowerUp.png", objTable = FuselagePowerUpSpawnTable, hasBody = true, density=.01, friction = 0.4, bounce = 0, isSensor=true, bodyType = "kinematic", group = localGroup, x=600, y=math.random(heli.y-200, heli.y+200), linearVelocityX=-200 } ) nextFuselagePowerUp=storyboard.gameVars.FuselagePowerUpFrequency --Now remove FuselagePowerUps out of the screen --for i = #spawnTable, 1, -1 do -- if spawnTable[i] ~= nil then -- if spawnTable[i].x \<-300 then -- display.remove(spawnTable[i]) -- table.remove(spawnTable,i) -- end -- end --end else if #FuselagePowerUpSpawnTable\>0 then if FuselagePowerUpSpawnTable[1].x \<-100 then --print("Total FuselagePowerUps Before: "..#FuselagePowerUpSpawnTable) display.remove(FuselagePowerUpSpawnTable[1]) table.remove(FuselagePowerUpSpawnTable,1) --print("Total FuselagePowerUps After: "..#FuselagePowerUpSpawnTable) end end nextFuselagePowerUp=nextFuselagePowerUp-1 end --myText.text = "FuselagePowerUps: "..#spawnTable end

oh yeah, and the forward declarations (I should have posted this on top… )

--Create a table to hold our spawns local FuselagePowerUpSpawnTable={} --Vars local nextFuselagePowerUp=0

Hi @guillopuyol,

I don’t see anything immediately “incorrect” except that “object” isn’t defined in the scope of the spawn function (and this shouldn’t be causing any issue between Mac and PC).

If it only happens when you try to display a sprite object, then I suspect there’s something wrong with how/when you set up the image sheet for the sprite… i.e., it can’t find the image sheet because of a scope issue, so it just fails to create the sprite and then errors out on line 245.

I’d suggest adding some “print()” lines in there and checking the value of each aspect as the function progresses up to line 245.

Sorry I couldn’t help more…

Brent

Thanks for the reply! I’m just confused on why it runs fine on PC but not on the Mac. If it was a scope issue wouldn’t it manifest itself regardless of the OS?