What actually happens in a script when the sim restarts?

Creating scripts
User avatar
Dale Innis
Posts: 41
Joined: Thu Mar 13, 2014 1:59 am
Has thanked: 84 times
Been thanked: 49 times

What actually happens in a script when the sim restarts?

Post by Dale Innis »

Sorry if this is a frequently-asked question or anything, but I haven't stumbled across an answer: what actually happens in a script when a Kitely sim shuts down, and later starts up again? Especially if the script is in an event handler (or a function called from one, or etc.) at the time.

I'm pretty sure a script doesn't get reset on sim restart, but does it re-enter whatever event handler it was in from the start?

Or does it skip the unexecuted part of that handler?

Or does it sometimes do one and sometimes the other?

Or does the sim not actually shut down until all the scripts have finished their current handlers (or timed out from the handler time limit)?

I've done some experiments, but I'm not sure I understand what I've seen. :) I think maybe I've seen a case where it looked like handlers were getting interrupted halfway through when the sim shut down, and not re-entered on sim restart, but I'm not certain.

Is this a known thing?

Thanks! -- Dale
These users thanked the author Dale Innis for the post:
Ilan Tochner
User avatar
Ilan Tochner
Posts: 6503
Joined: Sun Dec 23, 2012 8:44 am
Has thanked: 4942 times
Been thanked: 4454 times
Contact:

Re: What actually happens in a script when the sim restarts?

Post by Ilan Tochner »

Hi Dale,

Scripts are stopped whenever the world is stopped. I recommend you add checks for the region start event so that active scripts will be able to resume working after a world restart. See: http://wiki.secondlife.com/wiki/CHANGED_REGION_START
These users thanked the author Ilan Tochner for the post (total 2):
Snoots DwagonDale Innis
User avatar
Snoots Dwagon
Posts: 422
Joined: Mon Jul 30, 2018 9:45 pm
Has thanked: 442 times
Been thanked: 779 times

Re: What actually happens in a script when the sim restarts?

Post by Snoots Dwagon »

To add to Ilan's information (checking that WIKI page is a good idea)... if you have data that needs preserved over a sim restart, be sure it's hard-recorded rather than memory recorded.

For example, let's say you have a visitor counter and you wish to keep track of how many visitors have been on your region since its first day. You don't ever want to lose track of that count. So a line line:

visitorcount++; // won't work, because if the sim resets it will revert to 0.

However, if you hard-code it (storing it permanently) then even when the sim restarts the counter will pick up right where it left off. One way to do that is to store the information in the object description by using integer to string conversion. It's a bit more complex and takes more coding time, but since the visitor count will be permanently kept in the object's description field, and you will pull that number from the description field each time you add a visitor to it... that number will survive a sim reset.

Semi-code example:

visitor sensed
get existing visitor_count from description
add 1 to visitor_count
store visitor count in description

If you need to track several pieces of data this can become quite a bit more complex, with need to use list variables or parsing instead. Some data sets may be large enough to require notecard creation and reading.
These users thanked the author Snoots Dwagon for the post:
Dale Innis
~~~~~~~
I'm a dwagon in real life too. (Ask my sister, who totally agrees.)

~~~~~~~
User avatar
Dale Innis
Posts: 41
Joined: Thu Mar 13, 2014 1:59 am
Has thanked: 84 times
Been thanked: 49 times

Re: What actually happens in a script when the sim restarts?

Post by Dale Innis »

Snoots Dwagon wrote:
Tue Dec 29, 2020 9:18 pm
So a line line:

visitorcount++; // won't work, because if the sim resets it will revert to 0.
Why does it revert to zero? Is the script actually reset, as if an llResetScript() happened? Or are variables set to default values without the script resetting? Or something else?

This is why I asked the question. :) Because the details aren't at all clear to me.
User avatar
Snoots Dwagon
Posts: 422
Joined: Mon Jul 30, 2018 9:45 pm
Has thanked: 442 times
Been thanked: 779 times

Re: What actually happens in a script when the sim restarts?

Post by Snoots Dwagon »

Dale Innis wrote: Why does it revert to zero? Is the script actually reset, as if an llResetScript() happened? Or are variables set to default values without the script resetting? Or something else?
While others may have a more specific and exacting answer to this, to my understanding a region reset re-starts all scripts (yes, the same as llResetScript). So any data that has not been hard-saved will be lost, just as the contents of volatile RAM is lost when you shut down or restart your computer.

In standard OpenSim, consider a restart working in this way:

On the server screen there is your world on the screen in the form of a text box. This looks like a drop-back to the days of early DOS, but that pitiful-looking text box is what runs your entire world instance, performs teleports, communicates with avatars, etc. Basically... it IS your entire world, known as an "instance" of Opensim code running and doing what it's supposed to do.

When your sim resets or restarts, that instance box shuts down, and ceases to function, disappearing from the screen. It stores some vital information on the hard drive before it shuts off (and some vital information is regularly stored during world operation) but once that program shuts off, *poof*, data is gone, just like any other program that keeps data in RAM rather than a disc database.

So when the instance is turned back on (either by loading an OAR or doing a Kitely World Reset), things start re-building. Objects rebuild, textures are applied to objects, scripts and sound and animations are inserted into proper devices. Some effects carry over as "states" (not to be confused with state() event). For example, particles are maintained, texture animations are maintained, sit positions and other functions are maintained over a reset. Why some things are retained and others not is a mystery of the universe.

However script function is not maintained. They will loose temp variables, accumulated data, and in some instances (if the script is written poorly) will even lose functionality. An example of this is MONEY scripts that rather than being coded to fund and start themselves, instead require the merchant to manually authorize funding. Thus the oft-seen poorly-designed vendors that read "Vendor Offline" because the region restarted and the vendors are waiting for the merchant to manually press the "ON" button.

There is a lot going on during startup.

If you have a script that doesn't have to memorize anything... if it has hard-coded values within the object then such scripts should start working as soon as the world is fully reset. But if it lacks data need to run or requires personal interaction to work... such scripts need modded to work according to standard coding methodology. Ideally a script should never stop working just because a sim resets. However, they will lose any data held in RAM rather than hard-coded and preserved.
These users thanked the author Snoots Dwagon for the post (total 2):
Ilan TochnerDale Innis
~~~~~~~
I'm a dwagon in real life too. (Ask my sister, who totally agrees.)

~~~~~~~
User avatar
Dale Innis
Posts: 41
Joined: Thu Mar 13, 2014 1:59 am
Has thanked: 84 times
Been thanked: 49 times

Re: What actually happens in a script when the sim restarts?

Post by Dale Innis »

Thanks, that's really interesting.

I don't think it's literally a script reset, unless I'm completely misremembering things. I have scripts that handle being reset just fine, but get confused sometimes when the sim restarts. I guess I should add a changed() handler that does an llResetScript() on CHANGED_REGION_START to basically every script that might care. :)

(I also recall, although I can't find it right now, that I made a script that would count its restarts by saving the count in the description, as you suggested, and display it as a hovertext, and it never incremented even if the sim had been stopped and started multiple times.)

It would be really nice to know in technical detail exactly what happens to a running script (especially one that is in an event handler) when a sim stops and then starts again. Does anyone know where in the OpenSim source code to start looking for that? I don't know anything about the source tree, I have to admit.
User avatar
Snoots Dwagon
Posts: 422
Joined: Mon Jul 30, 2018 9:45 pm
Has thanked: 442 times
Been thanked: 779 times

Re: What actually happens in a script when the sim restarts?

Post by Snoots Dwagon »

This post deleted by Snoots because I was totally misinformed on this, even after all these years. Read John's post following. : )
Last edited by Snoots Dwagon on Thu Dec 31, 2020 7:51 pm, edited 1 time in total.
These users thanked the author Snoots Dwagon for the post:
Dale Innis
~~~~~~~
I'm a dwagon in real life too. (Ask my sister, who totally agrees.)

~~~~~~~
User avatar
John Mela
Posts: 91
Joined: Tue Feb 04, 2014 9:50 pm
Has thanked: 139 times
Been thanked: 127 times
Contact:

Re: What actually happens in a script when the sim restarts?

Post by John Mela »

Scripts are definitely not reset on a region restart (otherwise, there'd be no point in having change(CHANGED_REGION_START) because it would never happen!). The current script state is saved, and restored when the region sim boots. For the most part, scripts don't have to worry about region restarts, and can carry on processing as normal.

The RezMela system, for example, stores a lot of data in memory, which is intact after a restart. We'd certainly notice if that didn't happen!

However, dynamic textures may be lost (I believe because the sim cache is sometimes cleared during bootup), so our scripts react to CHANGED_REGION_START by recreating any dynamic textures. That's all.

To test this, try putting this script into a prim:

Code: Select all

integer x = 0;

default
{
    touch_start(integer n)
    {
        x++;
        llSay(0, (string)x);
    }
}
Each time you click the prim, it will say a higher number. Now restart your region, and try again. If the script had been reset, the numbers would start again from 1, but they don't, they carry on from before because the value of x is retained.
These users thanked the author John Mela for the post (total 2):
Snoots DwagonDale Innis
User avatar
Snoots Dwagon
Posts: 422
Joined: Mon Jul 30, 2018 9:45 pm
Has thanked: 442 times
Been thanked: 779 times

Re: What actually happens in a script when the sim restarts?

Post by Snoots Dwagon »

John, thank you for that information. I had been totally misinformed on this matter, even after all these years. This is good to know. Now like Dale, I'm curious what actually goes on behind the scenes to keep script data intact, since restarting a sim closes everything down pretty quickly.

I am also curious as to why a Visitor Counter script can lose its count when a world is restarted. If the data remains... why would it lose count? And why do vendor scripts often require new money permissions when a region restarts? (Bugs in the system software perhaps.)

Curiouser and curiouser. : )
These users thanked the author Snoots Dwagon for the post:
John Mela
~~~~~~~
I'm a dwagon in real life too. (Ask my sister, who totally agrees.)

~~~~~~~
User avatar
John Mela
Posts: 91
Joined: Tue Feb 04, 2014 9:50 pm
Has thanked: 139 times
Been thanked: 127 times
Contact:

Re: What actually happens in a script when the sim restarts?

Post by John Mela »

Yes, to know why a region restart breaks those scripts, we'd need to see the code.

One possible contender, which I've seen in freebie scripts in SL, is something like this:

Code: Select all

    changed(integer change) {
        llResetScript();
    }
which is an indiscriminate reaction to any change that fires that event, and I've known people to use to react to changes in inventory, or linking, or resizing - all sorts. Much better to test which change caused the event and react accordingly.
These users thanked the author John Mela for the post (total 2):
Snoots DwagonDale Innis
Post Reply