Can the Script.setInterval(function() be used in an entity?


#1

Hello,

I want to use Script.setInterval(function() in a script in an entity but somehow it is not working. If I run the script from the script editor it works. The log gives “testing testing …” every second. When I put the script in an entity (like a cube) nothing is happening? This is the code:

var multiple_timer = Script.setInterval(function()
{
print(JSON.stringify(“testing testing …”));

}, 1000);


#2

You can use Setinterval. i used it to.
I call with Script.setInterval(timerFun, OpenTimeSec); the function timerFun.
It’s example i do a bit out of my mind and peeking on some code. Hope it’s enough.

milliseconds is the timerinterval

timer=Script.setInterval(timerFun, milliseconds);

After the time that is set in miliseconds it’s calling this function.

    function timerFun()
    {
    }

You stop the timer with

Script.clearInterval(timer);


#3

Heya,

Entities for the script is loaded as a function that is called per each instance of the entity that calls it.
the function is expected to return an instance of the entity. (which is why there is only main script per entity)

Unlike UI thats just run down sequentially, Entity scripts work with a callback logic

In its base, raw form:

function(){  //  called when the script is loaded onto the entity, 
  return new function () { } // called by the entity when it is ready in the world.
 }

If you’d want to just have a simple setInterval, then use:

function(){  
  Script.setInterval(function() { }, 1000)
  return new function () { } 
 }

Just note that this will be called every single time the entity with the script is called, so eventually you will have multiple calls to the set interval function, even if other copies are removed.

Lets expand on the above concept if one wants to clean up the interval too after the entity is removed, and have multiple instances of the timer that are controlled:

function(){
   var Entity = function() {}
   // Define the object globally for each instance of the script
   Entity.prototype = {
     preload: function(entityId){
       this._interval = Script.setInterval(function() { }, 1000)
       // called when the prototype is first called. 
       // This could be considered as the constructor. EntityID is the object ID in the domain
       Script.scriptEnding.connect(this.cleanup);
     },
     cleanup: function() {
       Script.clearInterval(this._interval)
     }
   }
   // create a new instance
  return new Entity();
}

In the above case the instance of Entity is what other scripts will interact with.
So when object is created the function of the script will be run.

When Entity instance is returned to the World Entity, it will then check if it has the preload function and call it.


#4

Since when we need return for something like timer interval. my door works fine without the return. I where required to add the return since i tried to add Vive controller input.


#5

You dont, but he was asking about why the Script.setInterval wasnt working in his entity script.
So I assumed his entity script is incorrectly structured causing the script to not run properly in the entity.


#6

In case either of these answers gave you not enough information or too much information, here’s the skinny.

For entity scripts you need to wrap the code in a function.
(also there is no need to to stringify a string)

(function(){

var multiple_timer = Script.setInterval(function()
{
print(“testing testing …”);

}, 1000);

})

seems to work.


#7

Thanks guys, I’m a beginner so I used Adrians method and that works, When i’m more experienced I will come back and use Menithals more advanced stuff