Detection of Destroy Event, am I doing it wrong?

Okay, the scripts of concern are my CollisionScript.js and my TutorialScript.js

My goal is when the tutorialPoint entity gets destroyed (which happens when my character collides with the point), I want to set this.triggerTutorialDone (located in my TutorialScript) to true. I tried to various ways to do this (such as changing the interface directly in the collision script which ended up only showing the interface change for a split second) and events were next on my list.

I’ve checked out the previous conversation called, “Detecting if enemy is destroyed” but unfortunately the original posters project is no longer available. I only recently started to use playcanvas and a newbie at coding altogether so my understanding of the language is still a little wonky

CollisionScript.js

var CollisionScript = pc.createScript('collisionScript');

var count = 0;

// initialize code called once per entity
CollisionScript.prototype.initialize = function() {
    this.entity.collision.on('collisionstart',this.onCollisionStart,this);
    this.entity.collision.on('collisionend', this.onCollisionEnd, this);

    this.app.scenes.loadSceneData;
    this.entity.root.findByName('NumScore').element.text = count;
    this.sceneUntitled = this.app.scenes.find('Untitled');
    
    
};


CollisionScript.prototype.onCollisionStart = function(result){  
        if(result.other.name == 'Point'){
            this.entity.sound.play('Magic');
            result.other.destroy();

            count += 20;
            this.entity.root.findByName('NumScore').element.text = count;
        } else if (result.other.name == 'TutorialPoint'){
            this.entity.sound.play('Magic');
            result.other.destroy();

            count += 20;
            this.app.fire('tutorialTrigger', true);
            this.entity.root.findByName('NumScore').element.text = count;


        }

    
        if (result.other.name == 'TPL1'){
            this.app.scenes.changeScene('Map2');
    }

};

TutorialScript.js

var TutorialScript = pc.createScript('tutorialScript');
var playTrigger = false;

// initialize code called once per entity
//var pos1Script = pickedEntity.script.Pos1Attributes;

TutorialScript.prototype.initialize = function() {
    var characterEntity = this.app.root.findByName('MainChar');

    this.text1 = this.entity.findByName('Text1');
    this.text2 = this.entity.findByName('Text2');
    this.text3 = this.entity.findByName('Text3');

    characterEntity.script.characterControl.enabled = false;

    this.text1.enabled = false;
    this.text2.enabled = false;
    this.text3.enabled = true;
    this.triggerTutorialDone = false;

    this.pointEntity = this.app.root.findByName('TutorialPoint');
    this.pointEntity.on('destroy', function (tutorialTrigger){  
        this.triggerTutorialDone = tutorialTrigger;
        }, this);

};



// update code called every frame
TutorialScript.prototype.update = function() {
    var characterEntity = this.app.root.findByName('MainChar');
    var sceneUntitled = this.app.scenes.find('Untitled');

        if(sceneUntitled){
                this.text1.enabled = true;
        }   else {
                
    }

        if(this.app.keyboard.isPressed(pc.KEY_SPACE)){
            playTrigger = true;
        } else {

    }

        if(playTrigger === true) {
        characterEntity.script.characterControl.enabled = true;
        this.text1.enabled = false;
        this.text2.enabled = true;
    }   else {
}
        if(this.triggerTutorialDone === true) {
            this.text2.enabled = false;
            this.text3.enabled = true;
}
};


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

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

Hi @patsi and welcome!

Are you sure the event is returning true and not an entity or something? You can add console.log(tutorialTrigger) to see in the console of your browser, if it is executed and what it returns.

I assume you want to set this.triggerTutorialDone to true when the event is fired, so maybe you can do this directly instead of using tutorialTrigger.

1 Like