I’m looking to get my script to turn something on after a set time and then turn it off… I can get it to turn on but I can’t get it to turn off … any help would be greatly appreciated. Here is my script:
var TimerGraphs = pc.createScript('timergraphs');
TimerGraphs.attributes.add("graph", { type: "entity", title: "Graph" });
// initialize code called once per entity
TimerGraphs.prototype.initialize = function() {
this.timer = 0;
this.isTimerActive = true;
};
// Begin timer by calling this function
TimerGraphs.prototype.beginTimer = function(){
this.isTimerActive = true;
};
// update code called every frame
TimerGraphs.prototype.update = function(dt) {
if(!this.isTimerActive)
return;
this.timer += dt;
if (this.timer > 1) {
this.graph.enabled = !this.graph.enabled;
this.timer = 0;
this.isTimerActive = false;
if (this.timer > 5) {
this.graph.enabled = !this.graph.enabled;
this.timer = 0;
this.isTimerActive = false;
}
}
};
Hey, so, for now, the script will work for only the first if condition and it will never go to the second if state i.e if (this.timer > 5) because this.timer is initialized to 0 in the first condition every time. You will have to add another variable which checks first condition 1 time and then check second condition when first is fulfilled.
Assuming, you are enabling the graph entity in first condition and disabling it in second condition, here is how you can achieve it.
Have looked into your test project. It only works first time because you are not setting this.isTimerActive to true again. Either remove this condition
if(!this.isTimerActive)
return;
or add the logic to enable the isTimerActive Boolean