.lua:1051: bad argument #-2 to 'insert' (Proxy expected, got nil)

Can anyone please explain what this error could be? Just trying to insert an object into a group and sometimes get this:

... .lua:1051: bad argument #-2 to 'insert' (Proxy expected, got nil)
  1. In a nutshell it could be:

A. The group object reference is nil -OR-

B. You called insert with a dot instead of a colon  -OR-

C. The reference for the to be inserted object is nil.

  1. For better help, please show your (formatted) code (just the part that the error is for and where you created the object you inserted into the table).

I’m using it in a construct like this:

 if self and self.bulletTrailEffect then         self.bulletTrailEffect.deleteTimer=performWithDelay(500,deleteTrailNow,1)         self.bulletTrailEffect.deleteTimer.params=self.bulletTrailEffect         self.bulletTrailEffect.deleteTimer.smokeGroup=newGroup()         if self.bulletTrailEffect.deleteTimer then              self.bulletTrailEffect.deleteTimer.smokeGroup:insert(self.bulletTrailEffect) --this is the error line! Sometimes!         end         transition.to (self.bulletTrailEffect,{time=400,alpha=0})  end

Thank you for your help!

This variable name doesn’t make sense to me, did you mistype it?

 self.bulletTrailEffect.deleteTimer.smokeGroup

No. It’s like  this.
But I had a different group there before instead of this smokeGroup I did:

local stageGroup=display.getCurrentStage() stageGroup:insert(self.bulletTrailEffect)

which changed nothing regarding the described error

Actually, re-read the code.  You can’t do this, it doesn’t make sense:

self.bulletTrailEffect.deleteTimer=performWithDelay(500,deleteTrailNow,1) self.bulletTrailEffect.deleteTimer.smokeGroup=newGroup() -- WRONG

Can you tell me in a basic bullet list of steps what you are trying to do?

It looks like you have an object and you want to add a particle emitter to it, then delete that emitter after 500 ms.  Is that right?

self is an object group containing a weapon bullet image object.

I then create a smoke trail emitter for the bullet with self.bulletTrailEffect

I want to delete the emitter (and everything else bullet related) after 500ms

This would work better:

local function addTrail( obj ) if( obj == nil ) then error("Object is nil") end local emitter = display.newEmitter( ... ) -- fill is your own settings here if( emitter == nil ) then error("Emitter is nil") end -- parent is group obj is in. obj.parent:insert( emitter ) -- No need to track this or cancel it is safe to run even if obj is removed before -- it is called. timer.performWithDelay( 500, function() display.remove( emitter ) display.remove( obj ) end ) end -- later ... local tmp = display.newCircle( 10, 10, 10 ) addTrail( tmp ) 

Note: This doesn’t include code to make the emitter follow the object if it moves.

This would have the emitter follow the object
 

local function addTrail( obj ) if( obj == nil ) then error("Object is nil") end local emitter = display.newEmitter( ... ) -- fill is your own settings here if( emitter == nil ) then error("Emitter is nil") end -- parent is group obj is in. obj.parent:insert( emitter ) function emitter.enterFrame( self ) if( type(self.removeSelf) ~= "function" ) then Runtime:removeEventListener( "enterFrame", self return elseif( not obj.removeSelf or type(obj.removeSelf) ~= "function" ) then Runtime:removeEventListener( "enterFrame", self return end self.x = obj.x self.y = obj.y end Runtime:addEventListener( "enterFrame", emitter ) -- No need to track this or cancel it is safe to run even if obj is removed before -- it is called. timer.performWithDelay( 500, function() Runtime:removeEventListener( "enterFrame", emitter ) display.remove( emitter ) display.remove( obj ) end ) end -- later ... local tmp = display.newCircle( 10, 10, 10 ) addTrail( tmp )

Thank you for your fast and detailed help roaminggamer!

I wish you a Merry Christmas!

You’re welcome.

Ping back later if there is a problem with this solution. Remember, I don’t run these I just type them, so the ‘brain compiler’ may be faulty.

-Ed

I will rework my code after Christmas based on your code sample.

One more thing: can you please explain why the above newGroup code is wrong in the sample above?

It isn’t 100% wrong, but I wouldn’t feel good encouraging you to continue using the table returned by a timer that way.

I would treat the table returned by performWithDelay() with caution and not as a generic table. (Others may not agree).

The real issue I see is that the variable hierarchy is too deep for the purpose it is serving. You can greatly simplify adding, tracking, and removing emitters.

At the end of the day I suspect either the object you are adding the emitter to is not getting created, OR the emitter is not thus the failed insert.

Great info regarding this. Thank you very much!

  1. In a nutshell it could be:

A. The group object reference is nil -OR-

B. You called insert with a dot instead of a colon  -OR-

C. The reference for the to be inserted object is nil.

  1. For better help, please show your (formatted) code (just the part that the error is for and where you created the object you inserted into the table).

I’m using it in a construct like this:

 if self and self.bulletTrailEffect then         self.bulletTrailEffect.deleteTimer=performWithDelay(500,deleteTrailNow,1)         self.bulletTrailEffect.deleteTimer.params=self.bulletTrailEffect         self.bulletTrailEffect.deleteTimer.smokeGroup=newGroup()         if self.bulletTrailEffect.deleteTimer then              self.bulletTrailEffect.deleteTimer.smokeGroup:insert(self.bulletTrailEffect) --this is the error line! Sometimes!         end         transition.to (self.bulletTrailEffect,{time=400,alpha=0})  end

Thank you for your help!

This variable name doesn’t make sense to me, did you mistype it?

 self.bulletTrailEffect.deleteTimer.smokeGroup

No. It’s like  this.
But I had a different group there before instead of this smokeGroup I did:

local stageGroup=display.getCurrentStage() stageGroup:insert(self.bulletTrailEffect)

which changed nothing regarding the described error

Actually, re-read the code.  You can’t do this, it doesn’t make sense:

self.bulletTrailEffect.deleteTimer=performWithDelay(500,deleteTrailNow,1) self.bulletTrailEffect.deleteTimer.smokeGroup=newGroup() -- WRONG