[SOLVED] Press L to turn on light

So I have this spotlight, and im trying to set the intensity to 1 if you press L. I added a script component in it, and a script assigned to it called ‘Light.Js’. Here is the code that I have:

Light.prototype.update = function(dt) {
    var app = this.app;
    if (app.keyboard.wasPressed(pc.KEY_L)) {
        this.SpotLight.Light.intensity = 1;
    } 
};

And it produces this error:

Here is the project if it helps: le grand gameee, and any help is appreciated!

Hello @WHISPEROFWIND :wave:,

So instead of above line use following line according to your requirement,

:arrow_right: If you want to just turn on light and keep the light on forever forever.

this.entity.light.intensity = 1;

:arrow_right: If you want on/off light functionality.

this.entity.light.intensity = !this.entity.light.intensity;

1 Like

Thank you so much, it works now!

1 Like

Another question, if I wanted to add some type of health system to light, i.e, if the ‘battery life’ is not 0, then you can turn on the light, but if it hits 0, it turns off/if it’s already 0, it just doesn’t turn on, how can I do that?

@WHISPEROFWIND
I am not sure this is what you are looking for…

The code keeps the battery On for 5 sec. then it turn off automatically. You have to wait till the 5 sec before you can turn on again.

Try it.

var Light = pc.createScript('light');

// initialize code called once per entity
Light.prototype.initialize = function () {
    this.entity.light.intensity = 0;
    this.isLightOn = false;
    this.lightTimeCounter = 0;
    this.lightTimeCounterMax = 5;
};

// update code called every frame
Light.prototype.update = function (dt) {
    var app = this.app;

    if (app.keyboard.wasPressed(pc.KEY_L)) {
        if (this.lightTimeCounter == 0) {
            // if battery is complely off
            this.entity.light.intensity = !this.entity.light.intensity;
            this.isLightOn = this.entity.light.intensity;
        }
        else if (this.isLightOn == true) {
            // if battery is on but turn off in middle
            this.entity.light.intensity = 0;
            this.isLightOn = false;
        }
    }

    if (this.isLightOn == true) {
        // battery starts draining when it is on
        this.lightTimeCounter += dt;

        if (this.lightTimeCounter > this.lightTimeCounterMax) {
            this.entity.light.intensity = 0;
            this.isLightOn = false;
        }
    }
    else if (this.isLightOn == false && this.lightTimeCounter > 0) {
        // recharge battery again when it is off
        this.lightTimeCounter -= dt;
        if (this.lightTimeCounter < 0) {
            this.lightTimeCounter = 0;
        }
    }

    console.log("Battery Life: ", this.lightTimeCounterMax - this.lightTimeCounter);
};

1 Like

im trying to implement a ‘battery life’ system with the light, if that makes sense, so the battery life starts at 100, and then overtime it decreases to 0 but the battery only runs out when its being used, kinda like something you’d see with FNAF, if you get what i mean. if it hits 0, then you can’t use the flashlight anymore.

Refer above shared script which help you to create counter & light on off condition.

Where your counter will starts with 100.
In update function you have to decrease the counter overtime with the help of dt.

There you need to add check for Key press if counter is grater than 0 then it will turn on the light.

Will it help?

2 Likes

Sure, thanks!

1 Like

@WHISPEROFWIND
I am not sure you are able to solve the problem or not…
Just simply dropping the script which you required.

var Light = pc.createScript('light');

// initialize code called once per entity
Light.prototype.initialize = function () {
    this.entity.light.intensity = 0;
    this.isLightOn = false;
    this.lightTimeCounterInit = 100;
    this.lightTimeCounter = this.lightTimeCounterInit;
};

// update code called every frame
Light.prototype.update = function (dt) {
    var app = this.app;

    if (app.keyboard.wasPressed(pc.KEY_L)) {
        if (this.lightTimeCounter > 0) {
            // on off light
            this.entity.light.intensity = !this.entity.light.intensity;
            this.isLightOn = this.entity.light.intensity;
        }
    }

    if (this.isLightOn == true) {
        // battery starts draining when it is on
        this.lightTimeCounter -= dt;

        if (this.lightTimeCounter <= 0) {
            // battery will turn off automatically when it reaches to 0
            this.lightTimeCounter = 0;
            this.entity.light.intensity = 0;
            this.isLightOn = false;
        }
    }

    console.log("Battery Life: ", this.lightTimeCounter);
};

Hope it helps!

1 Like

thx you so much

1 Like

Hi @WHISPEROFWIND could you send me the link of your project to see what you did with the entities if you parsed or not, anyway if you don’t send me I’m happy to ask you a couple of things for my project.
Sincerely: Ricardo_Reyes