(Contains SSK2 Calls) Attempt to perform arithmetic on field width (a nil value)

Hello, I a trying to implement a health bar and I am having some trouble. 

--healthbar.lua local M = {} local healthBarObject local manageCurrentHealthBar local paint = {type = "gradient", color1 = {0.4, 0.8, 1}, color2 = {0.4, 0.5, 1}, direction = "down"} function M.new(group, object) healthBarObject = object local currentHealthBar = display.newRect(group, centerX, centerY, object.width / 2, 20) \<---- error line currentHealthBar.fill = paint currentHealthBar.id = "currentHealth" --currentHealthBar.enterFrame = manageCurrentHealthBar --listen("enterFrame", currentHealthBar) end manageCurrentHealthBar = function(self) --[[if self.id == "currentHealth" then end]] end return M --main.lua healthbar.new(group, ship)

I get an error pointing to the line that is marked above. I printed out the value of “object” and it is not nil. Any help would be great, thanks! 

That means, ‘object’ is no longer valid.  It was removed before the call to new()

You must be calling the function after deleting the object.

Check your object tracking code and make sure any references to that object are removed when you delete it.

Specifically, the display object was removed, then the you called healthbar.new() and passed in the object.

Because it was removed before the call, width, and all the other ‘Corona added’ fields are gone.

The way this can happen is as follows:

  1. You put a reference to OBJ in a table.

  2. You delete the object, but don’t remove the object from the table correctly.

  3. You iterate over the table later, find the ‘dead’ reference to the object and use it in a call.

Another way to have this happen is if you’re using global variables and have a reference to some object that has been deleted.

Since you’re using SSK,  you can take advantage of a helper: isValid.

First, try this to see what is going on:

function M.new(group, object) if( not display.isValid( object ) ) then print("The object parameter has been deleted and is invalid?" -- return nil end healthBarObject = object local currentHealthBar = display.newRect(group, centerX, centerY, object.width / 2, 20)

Running this will still crash, but it will verify the object is invalid as I suspect it is.

Then you can protect yourself from a crash like this (this is avoiding the issue, which is fine if you’re just doing this to learn more and for fun; on a professional project the right response would be to find the root cause):

function M.new(group, object) if( not display.isValid( object ) ) then print("The object parameter has been deleted and is invalid?" return nil end healthBarObject = object local currentHealthBar = display.newRect(group, centerX, centerY, object.width / 2, 20)

Note: This will return nil instead of a reference to the bar.

Thank you very much. I have also determined the root cause of the problem. I never returned the ship object in ship.lua, hence why it was invalid. :stuck_out_tongue:

This project will be somewhat professional, so I wanted to figure out the source of the problem.

That means, ‘object’ is no longer valid.  It was removed before the call to new()

You must be calling the function after deleting the object.

Check your object tracking code and make sure any references to that object are removed when you delete it.

Specifically, the display object was removed, then the you called healthbar.new() and passed in the object.

Because it was removed before the call, width, and all the other ‘Corona added’ fields are gone.

The way this can happen is as follows:

  1. You put a reference to OBJ in a table.

  2. You delete the object, but don’t remove the object from the table correctly.

  3. You iterate over the table later, find the ‘dead’ reference to the object and use it in a call.

Another way to have this happen is if you’re using global variables and have a reference to some object that has been deleted.

Since you’re using SSK,  you can take advantage of a helper: isValid.

First, try this to see what is going on:

function M.new(group, object) if( not display.isValid( object ) ) then print("The object parameter has been deleted and is invalid?" -- return nil end healthBarObject = object local currentHealthBar = display.newRect(group, centerX, centerY, object.width / 2, 20)

Running this will still crash, but it will verify the object is invalid as I suspect it is.

Then you can protect yourself from a crash like this (this is avoiding the issue, which is fine if you’re just doing this to learn more and for fun; on a professional project the right response would be to find the root cause):

function M.new(group, object) if( not display.isValid( object ) ) then print("The object parameter has been deleted and is invalid?" return nil end healthBarObject = object local currentHealthBar = display.newRect(group, centerX, centerY, object.width / 2, 20)

Note: This will return nil instead of a reference to the bar.

Thank you very much. I have also determined the root cause of the problem. I never returned the ship object in ship.lua, hence why it was invalid. :stuck_out_tongue:

This project will be somewhat professional, so I wanted to figure out the source of the problem.