Difference between revisions of "LlGetScriptState"

From Second Life Wiki
Jump to navigation Jump to search
(crash monitor example)
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{LSL_Function/inventory|name|uuid=false|type=script}}
{{LSL Function
{{LSL_Function
|inject-2={{LSL Function/inventory|script|uuid=false|type=script}}
|func_id=250|func_sleep=0.0|func_energy=10.0
|func_id=250|func_sleep=0.0|func_energy=10.0
|func=llGetScriptState|return_type=integer|p1_type=string|p1_name=name
|func=llGetScriptState|return_type=integer|return_subtype=boolean|p1_type=string|p1_name=script
|func_footnote
|func_footnote
|func_desc
|func_desc
|return_text={{HoverText|boolean|TRUE or FALSE}} that is {{LSLG|TRUE}} if script '''name''' is running.
|return_text=that is [[TRUE]] if the {{LSLP|script}} is running.
|spec
|spec
|caveats=*Will not report FALSE for a script that encountered a {{LSLGC|Error|run-time error}}, which greatly reduces the usefulness of this function.
|caveats
|constants
|constants
|examples
|examples=
<syntaxhighlight lang="lsl2">
default
{
    touch_start(integer num_detected)
    {
        integer numberOfScripts = llGetInventoryNumber(INVENTORY_SCRIPT);
 
        integer index;
        do
        {
            string scriptName = llGetInventoryName(INVENTORY_SCRIPT, index);
            integer scriptState = llGetScriptState(scriptName);
 
        //  default value
            string output = "FALSE";
        //  else
            if (scriptState) output = "TRUE";
 
            // PUBLIC_CHANNEL has the integer value 0
            llSay(PUBLIC_CHANNEL,
                "Script named '" + scriptName + "' has current script state '" + output + "'.");
        }
        while (++index < numberOfScripts);
    }
}
</syntaxhighlight>
<syntaxhighlight lang="lsl2">
// Monitor other scripts in the object, then report, restart or both if some have crashed
// NB: cannot distinguish a manually stopped script or restart one
integer TRY_RESTART = FALSE;
integer REPORT = TRUE;
 
default
{
    state_entry() {
        // monitoring rate = once per minute
        llSetTimerEvent(60);
    }
   
    timer() {
        integer i = llGetInventoryNumber(INVENTORY_SCRIPT);
        string script_name;
        list stopped;
       
        while(--i >= 0) {
            // the script will also monitor itself, but there is little reason to specifically skip
            script_name = llGetInventoryName(INVENTORY_SCRIPT, i);
            if(!llGetScriptState(script_name)) {
                if(TRY_RESTART)
                    llResetOtherScript(script_name);
                if(REPORT)
                    stopped += script_name;
            }
        }
        if(stopped) {
            string message = "The following scripts were crashed/stopped: " + llList2CSV(stopped) + ".";
            if(TRY_RESTART)
                message += " A restart was attempted.";
            llInstantMessage(llGetOwner(), message);
        }
    }
}
</syntaxhighlight>
|helpers
|helpers
|also_functions=*{{LSLG|llSetScriptState}}
|also_functions=
{{LSL DefineRow||[[llSetScriptState]]|}}
{{LSL DefineRow||[[llResetOtherScript]]|}}
|also_events
|also_events
|also_tests
|also_tests
|also_articles
|also_articles
|notes
|notes
|permission
|history=*There was a time when [[llGetScriptState]] would return [[TRUE]] even if the script had encountered a {{LSLGC|Error|run-time error}}. This is no longer the case.
|negative_index
|sort=GetScriptState
|sort=GetScriptState
|cat1=Script
|cat1=Inventory
|cat2=Inventory
|cat2=Script
|cat3
|cat3=
|cat4
|cat4=
}}
}}

Latest revision as of 11:34, 18 October 2023

Summary

Function: integer llGetScriptState( string script );

Returns a boolean (an integer) that is TRUE if the script is running.

• string script a script in the inventory of the prim this script is in

Caveats

  • If script is missing from the prim's inventory or it is not a script then an error is shouted on DEBUG_CHANNEL.
All Issues ~ Search JIRA for related Bugs

Examples

default
{
    touch_start(integer num_detected)
    {
        integer numberOfScripts = llGetInventoryNumber(INVENTORY_SCRIPT);

        integer index;
        do
        {
            string scriptName = llGetInventoryName(INVENTORY_SCRIPT, index);
            integer scriptState = llGetScriptState(scriptName);

        //  default value
            string output = "FALSE";
        //  else
            if (scriptState) output = "TRUE";

            // PUBLIC_CHANNEL has the integer value 0
            llSay(PUBLIC_CHANNEL,
                "Script named '" + scriptName + "' has current script state '" + output + "'.");
        }
        while (++index < numberOfScripts);
    }
}
// Monitor other scripts in the object, then report, restart or both if some have crashed
// NB: cannot distinguish a manually stopped script or restart one
integer TRY_RESTART = FALSE;
integer REPORT = TRUE;

default
{
    state_entry() {
        // monitoring rate = once per minute
        llSetTimerEvent(60);
    }
    
    timer() {
        integer i = llGetInventoryNumber(INVENTORY_SCRIPT);
        string script_name;
        list stopped;
        
        while(--i >= 0) {
            // the script will also monitor itself, but there is little reason to specifically skip
            script_name = llGetInventoryName(INVENTORY_SCRIPT, i);
            if(!llGetScriptState(script_name)) {
                if(TRY_RESTART)
                    llResetOtherScript(script_name);
                if(REPORT)
                    stopped += script_name;
            }
        }
        if(stopped) {
            string message = "The following scripts were crashed/stopped: " + llList2CSV(stopped) + ".";
            if(TRY_RESTART)
                message += " A restart was attempted.";
            llInstantMessage(llGetOwner(), message);
        }
    }
}

See Also

Functions

•  llSetScriptState
•  llResetOtherScript

Deep Notes

History

Search JIRA for related Issues

Signature

function integer llGetScriptState( string script );