LSL 101/Logic

From Second Life Wiki
Jump to: navigation, search


← Compile Time Errors ↑̲  LSL 101  ̲↑ Putting It All Together →

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.

 
string ownerName;
 
integer totalDonations;
 
integer topDonation;
integer lastDonation;
 
string topDonor;
string lastDonor;
 
vector colorWhite = <1.0, 1.0, 1.0>;
vector colorRed = <1.0, 0.0, 0.0>;
vector colorYellow = <1.0, 1.0, 0.0>;
 
float floattextAlpha = 1.0;// [0.0, 1.0]
 
default
{
    on_rez(integer start_param)
    {
        llResetScript();
    }
 
    changed(integer change)
    {
        if (change & CHANGED_OWNER)
            llResetScript();
    }
 
    state_entry()
    {
        key ownerKey = llGetOwner();
        ownerName = llKey2Name(ownerKey);
 
        llSetPayPrice(20, [10, 20, 50, 100]);
 
        llSetText(ownerName + "\ndonations so far: 0 L$\n ", colorWhite, floattextAlpha);
    }
 
    money(key id, integer amount)
    {
        string name = llKey2Name(id);
        string firstName = llGetSubString(name, 0, llSubStringIndex(name, " ") - 1);
 
        llSay(0, "Thank you for the donation, " + firstName + "!" );
 
        totalDonations += amount;
 
        lastDonation = amount;
        lastDonor = name;
        vector colorToUse = colorYellow;
 
        if (topDonation < lastDonation)
        {
            topDonation = lastDonation;
            topDonor = lastDonor;
            colorToUse = colorRed;
        }
 
        llSetText(ownerName
                    +"\ndonations so far: " + (string)totalDonations + " L$"
                    + "\nlast donation: " + (string)lastDonation + " L$ (" + lastDonor + ")"
                    + "\ntop donation: " + (string)topDonation  + " L$ (" + topDonor + ")",
                        colorToUse, floattextAlpha);
    }
}

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.

**NOTE! If you copy this script, the line for the llSetText was too long to properly display on this page so I had to create a new line! You will have to erase the extraneous spaces and forced return for the script to work properly.


Go back to Compile Time Errors or proceed onward to Putting It All Together - Conclusion.