Jump to content

User defined function doesn't bring the same result?


Estelle Pienaar
 Share

You are about to reply to a thread that has been inactive for 73 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hello, I have made a user defined function that does not bring the same result as writing the function each time separately in the script. I might have made a mistake but don't see what it could be. The script snippet should cycle through a list by deleting the first value of a list and then adding it to the end of the list. In this example this is done with two separate lists at the same time:

list content1 = ["1","2","3","4","5"];
list content2 = ["a","b","c","d","e"];

default
{
    state_entry()
    {
       llSetTimerEvent(5);
    }

    timer()
    {
                //script cycles the first list
                if(llGetListLength(content1) > 0)
                        {
                        //script saves the first entry of the list
                        string first_entry = llList2String(content1, 0);
                        //script deletes the first entry of the list
                        content1 = llDeleteSubList(content1, 0, 0);
                        //script adds deleted first entry to the end of the list
                        content1 += first_entry;
                        llOwnerSay("Debugging. New list order is: " + llList2CSV(content1));
                        }
                
                //script cycles the the list
                if(llGetListLength(content1) > 0)
                        {
                        //script saves the first entry of the list
                        string first_entry = llList2String(content2, 0);
                        //script deletes the first entry of the list
                        content2 = llDeleteSubList(content2, 0, 0);
                        //script adds deleted first entry to the end of the list
                        content2 += first_entry;
                        llOwnerSay("Debugging. New list order is: " + llList2CSV(content2));
                        }               
    }
    
    touch_end( integer num_detected )
    
    {
     llSetTimerEvent(0);   
    }
}

 

This snippet works as expected.

Then I have made a user defined function but here the script will only delete the first entry of the list and put it at the end of the list at the very first iteration. After that, the list gets "stuck" and is not changed anymore at each timer event.

cycleList (list c_list) {
    if(llGetListLength(c_list) > 0)
                        {
                        string first_entry = llList2String(c_list, 0);
                        c_list = llDeleteSubList(c_list, 0, 0);
                        c_list += first_entry;
                        llOwnerSay("Debugging. New list order is: " + llList2CSV(c_list));
                        }
}


list content1 = ["1","2","3","4","5"];
list content2 = ["a","b","c","d","e"];

default
{
    state_entry()
    {
       llSetTimerEvent(5);
    }

    timer()
    {
                //script cycles the first list
                cycleList (content1);
                
                //script cycles the the list
                cycleList (content2);
                                      
    }
    
    touch_end( integer num_detected )
    
    {
     llSetTimerEvent(0);   
    }
}

Can anyone spot the mistake, that I am making? I don't get it.

Link to comment
Share on other sites

40 minutes ago, Estelle Pienaar said:

Can anyone spot the mistake, that I am making? I don't get it.

LSL is pass-by-value, and will never pass-by-reference. In other words, cycleList (content1); cannot change the value of content1. you have to return the value and do an assignment:

list cycleList (list c_list) {
    if(llGetListLength(c_list) > 0)
    {
       string first_entry = llList2String(c_list, 0);
       c_list = llDeleteSubList(c_list, 0, 0);
       c_list += first_entry;
       llOwnerSay("Debugging. New list order is: " + llList2CSV(c_list));
       //return c_list; // returning here but not outside the if() will result in an error:
       // functions that return a value must return a value in all cases.
    }
    return c_list;
}
// later in the script:
some_list = cycleList(some_list);

Consider also:

default
{   state_entry()
    {   integer x=7;
        llOwnerSay("X="+(string)x);
        // scope-creation does not need a reason, like an if/while/for 
        {   integer x = 9; // removing 'integer' from this line leads to more 'expected' behavior.
            llOwnerSay("X="+(string)x);
        }
        // when scope is exited, x has its previous value: (because was re-declared in new scope)
        llOwnerSay("X="+(string)x);
    }
}

 

Edited by Quistess Alpha
bug fixes.
  • Thanks 3
Link to comment
Share on other sites

6 hours ago, Quistess Alpha said:

LSL is pass-by-value, and will never pass-by-reference. In other words, cycleList (content1); cannot change the value of content1. you have to return the value and do an assignment:

list cycleList (list c_list) {
    if(llGetListLength(c_list) > 0)
    {
       string first_entry = llList2String(c_list, 0);
       c_list = llDeleteSubList(c_list, 0, 0);
       c_list += first_entry;
       llOwnerSay("Debugging. New list order is: " + llList2CSV(c_list));
       //return c_list; // returning here but not outside the if() will result in an error:
       // functions that return a value must return a value in all cases.
    }
    return c_list;
}
// later in the script:
some_list = cycleList(some_list);

Consider also:

default
{   state_entry()
    {   integer x=7;
        llOwnerSay("X="+(string)x);
        // scope-creation does not need a reason, like an if/while/for 
        {   integer x = 9; // removing 'integer' from this line leads to more 'expected' behavior.
            llOwnerSay("X="+(string)x);
        }
        // when scope is exited, x has its previous value: (because was re-declared in new scope)
        llOwnerSay("X="+(string)x);
    }
}

 

Thanks!

Edited by Estelle Pienaar
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 73 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...