[Script] How to detect script reload etc. when script is inside entity?


#1

Looked at the docs, looked at the examples. tried a few things. But it seems to fail.
What i need is a function that get called when you reload a script or delete the object. etc. The script is running inside entity.


#2

There is a scriptEngine call that you could use.

Script.scriptEnding.connect(func);

Although for reseting entities i would suggest avoiding setting a state on start or stop because objects move over their life time and are started and stopped at different times between all the clients (as all scripts are run locally on every client connected, so all clients affect everything) and stopping an entity script could technically happen when someone leaves a domain

Instead, everything is always run top to bottom and Order of operations apply, until timer interrupts and a callback is called. So you can always assume when a script is reset, will run everything from the file inorder, until it sets callbacks.


#3

Yes, it is quite amazing that the chaos of unsynchronized multiscript activity works. I guess it is that by using an eventing model, only the scripts experiencing interactions wake up. Also one could use a semaphore in the userdata space, which means I truly hope setting the JSON data is idempotent.


#4

I tried that command, and my print did not write anything to the log. So it’s not working. And you really need a script end. for things like stopping a loopsound. otherwise you start to stack loopsound on lopsounds.

Mabye i forgot how to start it, remember the script is insde the entity.

Edi: I think i now see wht i do wrong. I go retest.t


#5

Yeah, thats fine use case, but do not expect to use it for states. It should trigger just fine in removing callbacks and stopping sounds… most of the time.


#6

Well, like my first test. It’s not working. unless @ctrlaltdavid edit reload is working different. Then the hifi edit tool. That still does not have a reload option.

Anyway, i tried this, no errors. but not text in log too.

(function(){        
    Script.scriptEnding.connect(function(){print("Ping");})
}).

Edit: ?v=1 in the hifi edit not gives any result to. So do i soemthing wrong. hifi broken , or is it not supported ?

Looked at old code, this must be right. with the exception that it’s running from inside entity. Something is going wrong there.

(function()
{     
    this.preload=function(entityID)
    { 
        print("preload");
    }    
    
    function scriptEnding() 
    {     
        print("Ping");
    }
    
    Script.scriptEnding.connect(scriptEnding);    
})

#7

for entities, you should use the ‘unload’ method –

so something like

(function(){
this.unload=function(){
print('entity was unloaded')
}
})

that unload happens whenever you get ‘too far away’ from an entity, either physically or because you switched domains or it got deleted or something. i wouldn’t try to do toooo much there because theres no guarantee it will all get run, and you don’t want to do things like delete the entity in that callback, since everyone will have unload happening to their own entities at different times

i usually use the Script.scriptEnding method for client scripts.


#8

also if you need to actually differentiate between script reloads, unloads, etc. – here’s a fairly-reliable heuristic technique:

(function() {
    return {
        preload: function(uuid) {
            this.prestate = Entities.getEntityProperties(uuid, ['script','scriptTimestamp']);
        },
        unload: function(uuid) {
            var state = Entities.getEntityProperties(uuid, ['script','scriptTimestamp']);
            if (state.script === undefined)
                print('unload -- entity has been DELETED');
            else if (this.prestate.script !== state.script)
                print('unload -- script is being REPLACED');
            else if (this.prestate.scriptTimestamp !== state.scriptTimestamp)
                print('unload -- script is being RELOADED');
            else
                print('unload -- OTHER', state.script, state.scriptTimestamp)
        }
    };
})

Scriptending not working in entity