nil values which should not be nil.. \platform\resources\init.lua?

Hi all,

Just got this error twice tonight, in completely different places in my code and referencing somewhere I’ve never seen before and cannot find:

\corona\corona\platform\resources\init.lua:202 / 189…?

I don’t think this value is / should be nil either.

Anyone got any ideas?

Thanks!!

initlua202

initlua202_2

That just means the error is being raised from a function that is fired from a timer or enterFrame event. The error is still in your code though.

For example, the second one means you are trying to apply a force to an object that doesn’t exist.

1 Like

Hi SGS,

Thanks for the response.
I don’t think I’m using any enterFrame’s or timers in this particular situation.

I’ve tried my best to trap the nil on the second example - the code is:

if e.sType == 8 then
	playSFX("gertrudeattackSound")
	if e ~= nil and e.rotation ~= nil and e.x ~= nil and e.y ~= nil then
		e:applyLinearImpulse(Sin((360 - e.rotation)*RadtoDeg) * 3750, Cos((360 - e.rotation)*RadtoDeg)* 3750, e.x, e.y)
	end
else

Could it still be that?

Nick

Why do you have so many checks in this if statement?

If all those are really necessary maybe you are not removing your objects correctly, that could explain the wrong call to applyLinearImpulse, I’m just not sure about the castTime.

1 Like

I have a function I use when there is the possibility an object reference may be invalid (added onto display.*):

function display.isValid( obj )
  return( obj.removeObject ~= nil and type(obj.removeObject) == "function" )
end

You could do this:

if( display.isValid(e) ) then
		e:applyLinearImpulse(Sin((360 - e.rotation)*RadtoDeg) * 3750, Cos((360 - e.rotation)*RadtoDeg)* 3750, e.x, e.y)
	end

Tip: Use better variable names. ‘e’ is pretty bad for future legibility.

1 Like

Hiya,

I’ve added the extra checks to trap this error above - obviously to no avail - and left them in for the screenshot / snippet so you could see nothing in the applyLinearImpulse statement could be nil.

Ordinarily I’d just have the If object ~= nil - in this case the object is e. I use e as a shortening for enemy as it’s typed so often in my game - but yes agree @roaminggamer it’s a poor variable name!! I’m not so bad elsewhere in code - promise!! :smiley:

Just FYI this object is the players familiar - Gertrude - she’s created at game load and not destroyed at any point. Does this help?

Thanks again for all the feedback and sorry for any confusion,

Nick

PS I’m starting to see init.lua referenced more and more suddenly - in this because the information included in error messages has been increased? I’ve just recently upgraded to version 2020.3609.

Checking if the object reference is nil won’t catch your issue.

The reference is NOT nil at the time you’re trying to use it. It is invalid. i.e. It has value in it that references a non-existent object.

What is on lines:

  • 4557
  • 4651
  • 4725
    in main.lua? As per the dialog, that is where the issue is or very near it.

You will get errors similar to that, this happens:

local obj = display.newCircle( ... )
-- Add body, etc. here

timer.performWithDelay( 1000, function() obj:applyImpulse( ... ) end )

display.remove( obj )

1-second later you’ll get a error.

This is effectively what is happening in your game. You have to hunt down:

  • Where you’re destroying objects and do a cleanup: Cancel listeners and timers.
  • Where you’re getting these errors (doing the actual action that is complained about) and check for valid objects.
2 Likes

Awesome advice - thank you.
I cancel timers on destroyed enemies but now you mention it I think there will be some listeners hanging around!
Might not have time today but I will do as you suggest and let you know how I get on.
With help from you gurus I’m getting there!
Thanks again :slight_smile:

PS 4557 is the line in question - the others are lines in AI functions which call the other AI functions.

Only problem here is as mentioned above the object in question is never destroyed…! :thinking:

I’ve been working on this today.
Still no further forward. I’ve removed listeners from other objects that needed it after your advice above - although I thought I had read somewhere, many moons ago, that sprite listeners were removed with sprites. Still better to be safe than sorry.
This object referenced above however is not removed at any point, and the code is not run through a timer, so I’ve no idea.
The only thing I can think of now is that this error has only started appearing after upgrade to the latest version of Solar 2D…

I’ve sussed it.
I’m now removing the physics body with a timer set to 50ms to stop the “can’t change physics bodies whilst number crunching error”.
Thus when summoning the entity whilst it still exists I’m actually now removing the physics body 50ms after creating it!
Can now resolve no problem.
Thanks so much for the assistance!!! :slight_smile:

The only display object listeners that are not removed automatically, are Global Event Listeners.

  • Those added with Runtime:addEventListener()
  • Calls to timer.performWithDelay()
1 Like

Thank you for letting me know.
Every day is a school day!!
:smiley: