jono3d
#1
I’m trying to make a script that turns off an object after a certain period of time. This is what I have so far:
struggling to get this working… is this in the right direction do you think?
var visibility = pc.createScript('visibility');
visibility.attributes.add('delay', {type: 'number', default: 0});
visibility.prototype.initialize = function()
// handle attribute changes
this.on('attr:duration', function (value)
{
this.delay(this.delay);
this.enabled = !this.enabled;
}
};
Hi @jono3d,
That event won’t fire on its own, you will have to program a timer.
Check this post for a nice implementation: [SOLVED] Timer Implementation - #4 by T_Froggo
1 Like
jono3d
#3
thank you.
completed script:
var Timer = pc.createScript('timer');
// initialize code called once per entity
Timer.prototype.initialize = function() {
this.timer = 0;
this.isTimerActive = true;
};
// Begin timer by calling this function
Timer.prototype.beginTimer = function(){
this.isTimerActive = true;
};
// update code called every frame
Timer.prototype.update = function(dt) {
if(!this.isTimerActive)
return;
this.timer += dt;
if (this.timer > 40) {
// 10 seconds has elapsed - do whatever you need here...
this.entity.enabled = !this.entity.enabled;
// Reset the timer
this.timer = 0;
this.isTimerActive = false;
}
};
works great!
1 Like