LSL 101/Logic

From Second Life Wiki
< LSL 101
Revision as of 13:51, 16 January 2011 by Sardonicus Direwytch (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

One thing to remember about scripting is that in some instances, the logic needs to be linear (meaning that some things have to come before others). In other instances, scripting logic operates much in the way that we think. To give you an example, calculators use linear logic. I'm going to give you an example of part of a tip jar script that I wrote. I will show you how you may think that it wouldn't work, but that is because you're using calculator logic. There is a logical process as to why the script works, as I will also show.

<lsl> integer i; integer m; integer n; string donorh; string donorl; string donorshort;

default {

   on_rez(integer start_param)
       llSetPayPrice(20, [10,20,50,100]);
       llSetText("Sard's Tip Jar\nDonations so far: L$0\n:'(\n ", <1.0,1.0,1.0>, 1.0);
   money(key id, integer amount)
           string thanks = llKey2Name(id);
           list tlst = llParseString2List(thanks, [""], [" "]);
           string thank = llList2String(tlst, 0);
           llSay(0,"Thank you kindly " + (string)thank + ", I promise not to spend this on booze.  *hickup*");
           if (amount > m)
               i += amount;
               m = amount;
               donorh = llKey2Name(id);
               llSetText("Sard's Tip Jar\nDonations so far: L$" + (string)i + "\nLast donation: L$" + (string)m + "\nLargest donation: L$" + (string)m + " (" + (string)donorh + ")", <1.0,1.0,1.0>, 1.0);
           else if (amount <= m)
               i += amount;
               n = amount;
               donorl = llKey2Name(id);
               llSetText("Sard's Tip Jar\nDonations so far: L$" + (string)i + "\nLast donation: L$" + (string)n + "\nLargest donation: L$" + (string)m + " (" + (string)donorh + ")", <1.0,1.0,1.0>, 1.0);

} </lsl>

I want to explain what part of this script is designed to do. The part we will be focusing on is the part that displays the largest donation and who made the largest donation.

Take a look at the event money and look down at the if, if else statement. Notice that before the if, else if statement, m has not been assigned a value. The if statement starts first by saying that if the amount is greater than m, then do this, then sates that if the amount is less than or equal to m, do this instead. You will next notice that m is defined within the if statement to be the integer amount. If you are thinking with linear calculator logic, you may be thinking "if it comes to comparing the current amount in the transaction to m, how can it know if it is larger than m if m is not yet assigned a value?"

Well, this is an instance where LSL scripts think like we do. If you notice at the beginning of the script, we defined (m) as a global variable (integer). (m) is automatically given the value of 0 as soon as the script runs. Any amount paid to the tip jar will be larger than 0, so then, within the script, (m) is defined as the integer of the amount donated. The hover text is set to display "Largest donation: L$(m)." This only works because (m) was created as a global variable. When somebody donates into the tip jar, the value of (m) is changed from 0 to the amount they donated. The next time somebody donates, the script compares the amount to (m). If the amount is larger than (m), it assigns the new amount to (m) using linear logic (in that the sub string defines (m) as being the amount).

You may have also noticed that the llSetText is exactly the same except for the part that says "Last donation: L$(n)" in both if statements under the money event. So what happens here is that if the amount is less than or equal to (m), it runs the same hover text, but amount of the donation is assigned to the integer (n) and that the donor's name is assigned to (donorl) instead of (donorh). The string to display the largest donation remains the same. So (n) and (donorl) do not get stated in the hover text. Since this section of the script does not change the amount of (m) or (donorh), they remain the same as they were before. However, the part of the hover text that displays the last donation now says that the last donation was (n) as opposed to (m). This is because the last donation (if less than m) was assigned to the integer (n) instead of (m).

One thing of note, it is not necessary to define the integer (n) or the string (donorl) as global variables, as it is only used in it's own section of script instead of multiple parts of the script where linear logic takes place. However, I defined them already as global variables so that within the main body of the script, things are kept brief, and lines are shorter (instead of having to type integer (n = amount), all I had to do was type (n = amount)). The reason I did this was so that the body of the actual working script was shorter and more concise. This makes it easier on me and easier to find mistakes in scripting, at least for me. This is purely an aesthetic choice on my part. As a comparison, I did change part of the script. You'll notice that in the if, if else statements, everything is quick and easy to read. My actual script has more to it, like the section above the if, if else statement that makes the tip jar thank the donor in global chat. You will notice this section of script defines a string, then a list, then another string (if you were wondering what this does, this causes the script to return the value of the users first name only, as opposed to full name). I could have defined the first string and the list as global variables. This would have made the actual working part of the script easier to read (for me). It would not have been necessary though, as it's not used elsewhere in the script. I just wanted to give you an example of that.