Trying to remove an object, that is localized to a specific function, in a separate function.

– removed –

Yay! It works! :smiley:
Since bullet.body was the object being referenced in my enemy tank collision function, it only makes sense that I would need to make the emitter a child of bullet.body :smiley:
lol gotta love how most of the time the answers are so simple… but at the same time I wish it was something more complex so I wouldn’t have to feel so dumb. :stuck_out_tongue: :wink:

Here is a short vid showing that it works, and a thank you!
video

Cheers!
 

-Saer

:smiley: :smiley: :smiley:

Glad it’s working :slight_smile:

  • C

You could assign the emitter as a child object of the bullet:

[lua]

bullet.emitter = emitter – put particle candy code here

[/lua]

Then in your collision function, you can access it via:

[lua]

event.target.emitter

– or

event.other.emitter

[/lua]

Thanks for your response, Nick.

I thought it already is…?
This is what I have in the shootBullet function, as far as the initial creation of the emitter:
[lua]
local emitterName   = “E”   … (#BEmitters + 1) – This will concatenate the current number of BEmitters to the end of the string, resulting in E1 for the first, E2 for the second, etc.

Particles.CreateEmitter(emitterName, 0, 0, 0, true, true)

bullet.emitter = Particles.GetEmitter(emitterName) – doesn’t this make the emitter a child object of the bullet?
[/lua]

The problem is since ‘emitterName’ and the display group ‘bullet’ are both localized to my shootBullet function, I can’t access it in my buildEnemyTank function via

Particles.StopEmitter(emitterName) display.remove(bullet.emitter)

And as I stated in the vid, adding a forward declaration at the top of my code doesn’t help. :\

-Saer

Can you not just put:

[lua]

bullet.name = emitterName

[/lua]

Then use that within your collision function. 

Just curious but what would that accomplish?

Sorry, but I’m not quite following…

The display group ‘bullet’ is localized within my shootBullet function, therefore any reference I try to make outside the function doesn’t work. And if I try making a forward reference to the display group, as seen in the video, I can only fire one bullet at a time.
If I try to shoot more than one at a time, it must be completely removing the emitter and it’s reference because it freaks out saying it can’t find the emitter’s parent object…?

lol idk.

-Saer

Edit: Here is a video response as well: video
(lol I know I’m just repeating a lot of what I said in the other video :wink:
 

I understand my last reply was only yesterday, however I am curious as whether or not you have any thoughts on a way I might solve this issue? If not I am going to completely re-think&re-write my entire setup, for firing a bullet, and I just wanted to check with you before I attempt to do so.

-Saer

So…

You’ve got local bullets. That’s fine.

You’ve got a collision function for the bullets. That’s fine.

You’ve got an emitter getting created when you’re firing a bullet. That’s fine.

You’ve got a name for an emitter. That’s fine.

You need a way to get the name of the emitter from the local object (bullet), and the object is created inside a function. That’s your question.

That’s what I’m gathering from your post. Correct?

If so, you probably already know that you can access the local object in it’s collision function. It’s under event.target (or maybe self, if it’s not there). So that’s how you get the bullet itself. As for the emitter, you can make the name of the emitter an element of the bullet (As a side note, unless you’re not taking anything out of the BEmitters table, I’d use a separate counter for the name number):

[lua]

– Earlier:

local emitterCount = 0

– In bullet fire function

emitterCount = emitterCount + 1 – Increment emitterCount, which you’ll use for titling the emitters

local emitterName = “E” … emitterCount

bullet.emitterRef = emitterName

Particles.CreateEmitter(emitterName, doodad, thingy, somethingElse) – I have no idea how ParticleCandy works :slight_smile:

[/lua]

Then, when you want to stop the emitter (in the collision function?), do this:

[lua]

– I’m assuming ParticleCandy has a delete command…?

Particles.DeleteEmitter(event.target.emitterRef)

– (or stop and remove, if there is no delete equivalent)

[/lua]

Hope this helps!

  • Caleb

Thanks for your response, Caleb.

You would be correct.
Since I have the display group ‘bullet’ localized to the shootBullet function, I need a way to access the localized emitter attached to a fired bullet_._ The collision function that triggers the explosion 'n such is inside my buildEnemyTank function.

Anyways, I made the changes you suggested.
I now have this in my shootBullet function:
[lua]

      local bullet = display.newGroup()

      emitterCount = emitterCount + 1

      local emitterName = “E” … emitterCount
     
      – Bullet Emitter
      Particles.CreateEmitter(emitterName, 0, 0, 0, true, true)
      bullet.emitter = Particles.GetEmitter(emitterName)
      bullet.emitterRef = emitterName

[/lua]

Concerning the emitter, this is all I have inside the tank collision function:
Particles.DeleteEmitter(event.target.emitterRef)

When I shoot enemy tank I get this error:

"/lib_particle_candy.lua
Line: 354

Attempt to concatenate local ‘name’ (a nil value)

stack traceback:
    [C]: ?
    …lication Projects/DoodleTanks/lib_particle_candy.lua:354: in function ‘DeleteEmitter’
    …/Desktop/Application Projects/DoodleTanks/level4.lua:543: in function <…/Desktop/Application Projects/DoodleTanks/level4.lua:520>
    ?: in function <?:218>
"

It’s referring to my attempt to delete the emitter.
Also, the line it’s referencing in lib_particle_candy.lua is this:
print ("!!! Particles.DeleteEmitter(): COULD NOT DELETE EMITTER ‘"…name…"’ (NOT FOUND)")

Which resides in the DeleteEmitter function.

-Saer

 

I see in your code that you add the collision listener to bullet.body. So you would need to either set the name and emitter using bullet.body.name and bullet.body.emitter, or add the collision listener to bullet, not bullet.body.

I made these changes:
Particles.CreateEmitter(emitterName, 0, 0, 0, true, true) – Uses the base name (generated dynamically with each tank)
bullet.body.emitter = Particles.GetEmitter(emitterName)
bullet.body.emitterRef = emitterName

Got the same error.

Sorry to repeat myself (lol I’m not really sure what else to say) but my bullet’s collision function does Not have an ‘if’ statement for checking if it hit an enemy tank. My enemy tank.body collision function however, is checking for whether or not it collided with a bullet.
– not sure if that would matter –

-Saer

Edit: Collisions seem to working just fine, it’s trying to reference the emitter that is the problem.
I’m assuming I got that error 'Attempt to concatenate local ‘name’ (a nil value)’ because , for whatever reason, it doesn’t know what I’m trying to do when I put this: event.target.emitterRef
 

Is this deletion in the bullet’s collision function or the tank’s collision function?

If it’s in the tank’s, of course you’d need to use event.other (event.target -> tank).

If it’s in the bullet’s, try printing out what the emitterRef is and seeing if it returns as nil.

  • C

The tank’s collision function.
That’s what I thought so I already tried this -
Particles.DeleteEmitter(event.other.emitterRef)

And still got the same error.

If I try to put an ‘if’ statement inside the bullet’s collision function (which would simply check if event.other.class = “EnemyTank” and if so then delete the emitter) it doesn’t’ even register. As if the tank’s collision function overrides what should happen to the bullet

-Saer

It seems you’re getting the wrong object for event.other.

Make sure that event.other is actually the part of the bullet with the emitterRef, and try printing event.other.emitterRef (or if the collision function is for the bullet’s “body” object, use event.other.body.emitterRef).

  • C

In my enemy tank collision function, I have these two lines:
print("Event.other:   ", event.other.emitterRef)
Particles.DeleteEmitter(event.other.emitterRef)

The print statement is returning nil…?

-Saer

And you’re making sure that this chunk is only executing when the tank collides with a bullet?

If it’s executing this same section when the tank collides with anything, that could also be the problem.

  • C

The first three lines in my enemy tank collision function are the following:

local onEnemyTankCollision = function(self, event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local parentTank = enemyTanks[self.\_index] &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (event.phase == "began" and event.other.class == "bullet" or event.other.class == "PTmine") then &nbsp;

Could the issue be because I’m also checking for a different object?

-Saer

Edit: Nope. I removed or event.other.class == “PTmine” and I still got the error.

You probably ought to make one section (event.other.class == “bullet” or event.other.class == “PTmine”) that destroys the tank,  another (event.other.class == “bullet”) for bullet-specific collisions, and another for PTmine collisions.

But as long as a mine isn’t colliding when you get the error, that shouldn’t be the problem. Try doing this:

[lua]

– In bullet creation function

bullet.body.objectID = “bullet body”

[/lua]

Then print event.other.objectID and see if it gives anything. If it prints nil, you’re not picking up a bullet body collision, so it has no emitterRef.

  • C

Okay, so if I run the sim with the collision function checking for PTmine or without checking for PTmine I get the same thing:
2013-10-11 11:34:59.637 Corona Simulator[20371:e03] event.other objectID:       bullet body
2013-10-11 11:34:59.637 Corona Simulator[20371:e03] Event.other:       nil
2013-10-11 11:34:59.637 Corona Simulator[20371:e03] Runtime error
…lication Projects/DoodleTanks/lib_particle_candy.lua:354: attempt to concatenate local ‘name’ (a nil value)

-Saer

Using these three lines:

print("event.other objectID:   ", event.other.objectID)
print("Event.other:   ", event.other.emitterRef)
Particles.DeleteEmitter(event.other.emitterRef)

Could it be returning nil because emitterRef equals a value that was specified in another function and since I’m trying to access it in a separate function it for whatever reason cannot?
I don’t know the inner-workings behind how objects communicate between functions via calls 'n such, so sorry if what I just said is way wrong. :wink:

Just to clarify, here’s what your bullet code should be:

[lua]

– Bullet function

emitterCount = emitterCount + 1

local emitterName = “E” … emitterCount

bullet.body.emitterRef = emitterName

Particles.CreateEmitter(emitterName, [parameters])

[/lua]

  • C