Jump to content

Newbie Question - User defined Functions


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

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

Recommended Posts

I know I'm making a fundamental error here but I cant see what I'm doing wrong.  I have written a little bit of script to learn about some list functions.  As there was a repeat of a chunk of code I thought I'd make it into a function (displaylist) and just call the function twice but it is not picking up the variables even though I made them global.  I know I'm being a chump here by missing something obvious.

Code without the function which works fine:

list my_list;
integer i;
integer end;

default
{
    touch_start(integer total_number)
    {
       list my_list = [
        "1023:1000", "Reminder 1", llGetOwner(),
        "0712:1800", "Reminder 2", llGetOwner(),
        "0123:1153", "Reminder 3", llGetOwner(),
        "0123:0000", "Reminder 4", llGetOwner()
        ];

        i = 0;
        end = llGetListLength(my_list);
        
       for (; i<end; ++i)
        {
            llOwnerSay(llList2String(my_list,i) + ", "+ llList2String(my_list,i+1) +  ", "+ llList2String(my_list,i+2)   );
            i=i+2;
        }
               
       my_list = llListSort(my_list, 3, TRUE) ;
        
        llOwnerSay("------------------");
 
        i = 0;
        end = llGetListLength(my_list);       
        for (; i<end; ++i)
        {
            llOwnerSay(llList2String(my_list,i) + ", "+ llList2String(my_list,i+1) +  ", "+ llList2String(my_list,i+2)  );
            i=i+2;
        }
    }
}

 

Code with function displaylist()

list my_list;
integer i;
integer end;

displaylist()
{
  i = 0;
  end = llGetListLength(my_list);
  for (; i<end; ++i)
      {
       llOwnerSay(llList2String(my_list,i) + ", "+ llList2String(my_list,i+1) +  ", "+ llList2String(my_list,i+2)   );
       i=i+2;
       }
}

default
{
 
    touch_start(integer total_number)
    {
       list my_list = [
        "1023:1000", "Reminder 1", llGetOwner(),
        "0712:1800", "Reminder 2", llGetOwner(),
        "0123:1153", "Reminder 3", llGetOwner(),
        "0123:0000", "Reminder 4", llGetOwner()
        ];

       displaylist();
       
       my_list = llListSort(my_list, 3, TRUE) ;
       
       llOwnerSay("------------------");
        
       displaylist();
    }
}

 

 

Edited by GloriaGlitter
clarification
Link to post
Share on other sites

with the for loop then we need to step by 3 (the width of the list stride)

for (i = 0; i < end; i += 3)
{
    llOwnerSay(llList2String(my_list,i) + ", "+ llList2String(my_list,i+1) +  ", "+ llList2String(my_list,i+2));
}


i = 0; i+1 = 1; i+2 = 2;
i += 3; 0 + 3 = 3
i = 3; i+1 = 4; i+2 = 5;
i += 3; 3 + 3 = 6;
i = 6; i+1 = 7; i+2 = 8;
   
and so on

 

 

  • Like 1
Link to post
Share on other sites

 

2 hours ago, GloriaGlitter said:

touch_start(integer total_number)
    {
       list my_list = [
        "1023:1000", "Reminder 1", llGetOwner(),
        "0712:1800", "Reminder 2", llGetOwner(),
        "0123:1153", "Reminder 3", llGetOwner(),
        "0123:0000", "Reminder 4", llGetOwner()
        ];

You defined "my_list" as a global in the header section but you also redefine "my_list" with the list keyword in the touch_start event. That second time that "my_list" is defined in the event creates a local version of the list that is limited solely to the event and does not get passed to the function when it's called.  So when you try to set the "end" variable in the function with the call to get list length it's using the empty global "my_list" and is set to zero.  The for loop does its first check and since i equals end (both zero) the for loop exits immediately and the function exits normally.  It displayed nothing since there was nothing to display.  twice. 

If you emove the keyword "list" here (as shown here), the script and function will work as epxected.  this one is a bit hard to see because it works without the function. the creation of the local instance of 'my_list' is hidden and because everything is local to the touch_start event, it all works like you wanted it to.

touch_start(integer total_number)
    {
         my_list = [
        "1023:1000", "Reminder 1", llGetOwner(),
        "0712:1800", "Reminder 2", llGetOwner(),
        "0123:1153", "Reminder 3", llGetOwner(),
        "0123:0000", "Reminder 4", llGetOwner()
        ];

 

edit:  to clear up some of the wording and to add a few clarifying comments that I realized after the fact were needed.

 

Edited by Anna Salyx
  • Like 3
Link to post
Share on other sites

if JSON doesnt freak you out, you could do something like this small example...

( you can drop this in a box, touch it, then do ... /99 test ... to see it work :P )

string Reminders;

init()
{  Reminders = llList2Json( JSON_OBJECT, [] ); 
}
addReminder( string name , string dateTime, string text )
{
     if (llJsonGetValue ( Reminders, [name] ) == JSON_INVALID)  // if there is not a JSON key-value pair in the JSON object, add one
      {   Reminders = llJsonSetValue ( Reminders, [name,  "Date_Time"], dateTime);     
          Reminders = llJsonSetValue ( Reminders, [name,  "Reminder_Text"], text);                
      }
}
default
{
    state_entry()
    {  llListen(99,"","","");     
    }
    touch_start(integer total_number)
    {   llOwnerSay("Populating...");
        string name = "Your Name";
        string dateTime =  "2020-08-13T16:33Z";
        string text = "SHopping Event";   
        addReminder( name , dateTime,  text );
    }
    listen( integer vIntChn, string vStrNom, key vKeySpk, string vStrMsg )
    {
        if( vIntChn == 99)
        {    string info1 = llJsonGetValue ( Reminders, [ "Your Name", "Date_Time"]) ;
             string info2 = llJsonGetValue ( Reminders, [ "Your Name", "Reminder_Text"]) ;
             string info3 = llJsonGetValue ( Reminders, [ "Your Name" ]) ;
             llOwnerSay("got:\n1. " +  info1 + "\n2. " + info2 + "\n3. " + info3);             
        }
    }
}

 

Edited by Xiija
Link to post
Share on other sites
You are about to reply to a thread that has been inactive for 113 days.

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...