Detecting if enemy is destroyed

so would i want to detect if the entity is destroyed from another entity, with a script, would this work
this.Car = this.entity.findByName(‘car’);

if( this.Car.destroyed()) {
//than this happens

Hello @David_Warren,

Actually, if you take a look at the PlayCanvas documentation there is no destroyed method in the Entity class.

But, there is a destroy event that you can use in your case:

You can call this event and write your logic inside it. Something like this:

entity.on("destroy", function (e) {
    //do something
});
1 Like

thx, ill give it a run

1 Like

ok i have 2 entities that need to be seonsored, i want a screen to pop up if both of them are destroyed, would i do M1.on(“destroy”, function (e) {
M2.on(’'destroy", function (e) {
screen pop up.

and i wont know which order they will be killed, would that work

You can also create two flags and set these on false. When M1 is destroyed you set flag1 on true. When M2 is destroyed you set flag2 on true. After a destroy you check if both flags are on true, and if so then you show your pop up.

could you give an example. i understand what your saying but would you do it all in one script? or 2

Its all possible. How many scripts do you use for it?

id use one. i would attach the script to the ‘You Win’ screen, then if both are dead and both flags are true it, the screen would teleport to the front of the camera here’s my project, i have not started that script yet, feel free to give it a go

https://playcanvas.com/editor/scene/906840

I see you have a lot of scripts.
You have to set both flags before the initialize function in a general script like this:

var Win = pc.createScript('win');

var flag1 = false;
var flag2 = false;

// initialize code called once per entity
Win.prototype.initialize = function() {
    
};

// update code called every frame
Win.prototype.update = function(dt) {
    
};

And in the script where you destroy the enitity you set one of the flags on true.
After that you do the check if both flags are on true and show the pop up.

I must say that there are many other ways to do this.
It is just what you find the easiest to understand.

makes sense, the destroy function is not working for me, i tried the destroy function e, but i get no output from when my character is destroyed

I think then you are missing something. You have to create that function by yourself and then you can use it when you need it. Maybe you can show how you do it.

ok thanks, ill update you

i still counld not get it to work, i tried to add the function at the initialize stage but did not work

var Died = pc.createScript('died');

// initialize code called once per entity
Died.prototype.initialize = function() {
    
};

// update code called every frame
Died.prototype.update = function(dt) {
    

    var e = this.entity.findByName('Player 2');
   this.entity.on("destroy", function (e) { 
    var m = this.app.root.findByName('Monster1');
    m.rigidbody.teleport(-12, 1, 0);
    
    });
   

    
       
    
};

// swap method called for script hot-reloading
// inherit your script state here
// Died.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Try reading this -https://developer.playcanvas.com/en/user-manual/scripting/communication/. Also, I would recommend that you don’t name both your function argument and your variable the same. Change the name of the variable.

that did not help me because it does not relate to my case but i solved my problem, and thx, i realized that my health bar triggered my player to die, so i linked up a script to activate when my health is 0 than the script would display the screen ‘U Died’ much easier way that trying to use event

        this.HealthP.setLocalScale(scale);
        
        if (scale.x < 0.01) {
            scale.x = 0;
             var Die = this.app.root.findByName('Walls4');
            Die.addComponent("script");
             Die.script.create("died");
            this.entity.destroy();
          
             
            
        if (scale.x > 1) {
           scale.x = 1;
                
                
              }
            
          }
        if (this.takeDamage > 0) {
            scale.x -= this.takeDamage;
            this.takeDamage = 0;
        
        }
   
    }
    this.HealthP.setLocalScale(scale);
    
};

};

1 Like