Enttity not showing up as disabled in code

Hello, all.
I am working on a boss fight that has a looping section. The first part checks if it is hit 5 times, then goes to the second part, which enables some stuff, and then checks if something is disabled.

var Bossfightv2 = pc.createScript('bossfightv2');
Bossfightv2.attributes.add('ff', { type: "entity" });
Bossfightv2.attributes.add('rod1', { type: "entity" });
let stage = 1;
let hits = 0;
let counts = 0;
// initialize code called once per entity
Bossfightv2.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

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

    if (stage === 1) {
        if (hits === 5) {
            stage = 2;
            hits = 0;
        };
    };
    ///////////////////////////
    if (stage === 2) {
        this.ff.enabled = true;
        this.rod1.enabled = true;
        if (this.ff.enabled === false) {
            stage = 1;
        };
    };

    ///////////////////////////

    /////////////////////////

};

Bossfightv2.prototype.onCollisionStart = function (result) {
    if (result.other.tags.has('AA')) {
        hits++ + 1;
        console.log('hit' + hits);
    };
};
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// Bossfightv2.prototype.swap = function(old) { };

// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/

line 25 is what is not working. It doesn’t go back to Stage 1 if the force is disabled.
Thanks for any help you could give

Hi @Codeknight999!

I think this is as expected, because you enable this.ff on line 23. That means when the script reached line 25, this.ff is always enabled. Probably you can change the order of your lines. For example try to execute line 23 and 24 after your statement of line 25.

I tried that and it almost worked, I saw it enable for a split second and then it disappeared.

Sorry, that’s as expected as well. Maybe you need to place line 23 and 24 in the statement of line 15?

Should we just re-make the script in the right order?

I have all the code necessary but don’t know the order.

I don’t know your logic, but you need to imagine that the script is executed from top to bottom.

Correct, basically the script needs to: 1) wait till it gets hit 5 times and then 2)enable the forcefield and wait for the rod1 entity to be disabled, and then disable the forcefield, It should go through those 2 things 4 times. A bonus would be that it keeps track of wich step out of the 8 it is on. (8 being the 4 times for the 2 things that need to happen)

What about this?

// update code called every frame
Bossfightv2.prototype.update = function (dt) {
    if (stage === 1) {
        if (hits === 5) {
            stage = 2;
            hits = 0;
            this.ff.enabled = true;
            this.rod1.enabled = true;
        }
    }

    if (stage === 2) {
        if (this.ff.enabled === false) {
            stage = 1;
        }
    }
};

This sounds a little bit complex, while you currently enable both entities at the same time?

It is a bit complex, but is there a way to do it?

Sure, but I suggest to start simple so you have the basics stable first.

1 Like

Just got back home, I tested the script, and the second time when you shoot it 5 times the forcefield enables for a split second then disables. I will try something and get back to you.

I don’t see where you disable it.

I figured it out, I had another script active that did that. Sorry. I tried the code again and now the force field isn’t enabling.

Fixed the forcefield.

I have code on the lightning to disable it, so all we have to do is get the bossfightv2 script to see that and then disable the force field. If you have any ideas they would be appreciated, but I will do some testing.

I modified the code to disable the forcefield:

Bossfightv2.prototype.update = function (dt) {
    if (stage === 1) {
        if (hits === 5) {
            stage = 2;
            hits = 0;
            this.ff.enabled = true;
            this.rod1.enabled = true;
        }
    }

    if (stage === 2) {
        if (this.rod1.enabled === false) {
            this.ff.enabled = false;
            stage = 1;
        }
    }
};

All we have to do is make it so the forcefeild can stay enabled the second time around.

I dont know how to do that. Do you have any ideas, @Albertos?

I don’t know what you mean with ‘the second time around’.

Im trying to make it so you can go through the stage 1 and stage 2, 4 different times. The first time it worked, by the second time around, I meant the second time I went through stage 2.