Difference between revisions of "User:Toady Nakamura/Simple Recording Tipjar"

From Second Life Wiki
Jump to navigation Jump to search
m (rewrote second example script using variable names that actually mean something and improved readability and memory usage)
Line 44: Line 44:


<lsl>  
<lsl>  
integer i;               // donations so far
string ownerName;
integer m;               // high gift amount
 
integer n;               // last gift amount if not high gift
integer totalDonations;
string donorh;           // high donor's name
 
string donorl;           // donor's name
integer topDonation;
string owner;            // owner's name
integer lastDonation;
vector color1 = <1,1,1>; // one color
 
vector color2 = <1,0,0>; // another color
string topDonor;
vector color3 = <1,1,0>; // one more color!
string lastDonor;
float alpha = 1.0;       // intensity of set text (zero to 1.0)
 
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
default
{
{
    on_rez(integer start_param)
    {
        llResetScript();
    }
    changed(integer change)
    {
        if (change & CHANGED_OWNER)
            llResetScript();
    }
     state_entry()
     state_entry()
     {
     {
         owner = llKey2Name(llGetOwner()); // get the owner's name
         key ownerKey = llGetOwner();
         llSetPayPrice(20, [10,20,50,100]); // set the box of possible amounts with $20L as least
        ownerName = llKey2Name(ownerKey);
         llSetText("MY Tip Jar\nDonations so far: L$0\n:'(\n ", color1, alpha); // set text
 
         llSetPayPrice(20, [10, 20, 50, 100]);
 
         llSetText(ownerName + "\ndonations so far: 0 L$\n ", colorWhite, floattextAlpha);
     }
     }
 
     money(key id, integer amount)
     money(key id, integer amount)
     {
     {
         string thanks = llKey2Name(id); // gets id from money event, converts to name string
         string name = llKey2Name(id);
         list tlst = llParseString2List(thanks, [""], [" "]); //divide the list using blank space as separator
         string firstName = llGetSubString(name, 0, llSubStringIndex(name, " ") - 1);
        string thank = llList2String(tlst, 0); // use only the first name part
 
         llSay(0,"Thank you kindly, " + (string)thank + "!"); // Acknowledge gift in chat
    // PUBLIC_CHANNEL has the integer value 0
         llSay(PUBLIC_CHANNEL,
            "Thank you for the donation, " + firstName + "!");


         if (amount > m) // if gift is higher than HIGH GIFT ... (remember m starts out at zero)
         totalDonations += amount;
         {
 
            i += amount; // add the amount to donations so far
         lastDonation = amount;
            m = amount; // holds the amount of this gift & changes the former high gift to the new high gift amount
        lastDonor = name;
            donorh = llKey2Name(id); // set the high donor's name
        vector colorToUse = colorYellow;


        // use all that to make the set text display
         if (topDonation < lastDonation)
            llSetText(owner +"'s Tip Jar\nDonations so far: L$" + (string)i +
                                        "\nLast donation: L$" + (string)m +
                                        "\nLargest donation: L$" + (string)m  +
                                        " (" + (string)donorh + ")", color2, alpha);
         }
        else if (amount <= m) // if gift is not higher than HIGH GIFT
         {
         {
             i += amount;// add the amount to donations so far
             topDonation = lastDonation;
             n = amount; // holds the amount of this gift.
             topDonor = lastDonor;
 
             colorToUse = colorRed;
        // use all that to make the set text display
             donorl = llKey2Name(id); // set donor name
            llSetText(owner +"'s Tip Jar\nDonations so far: L$" + (string)i +
                                      "\nLast donation: L$" + (string)n +
                                      "\nLargest donation: L$" + (string)m +
                                      " (" + (string)donorh + ")", color3, alpha);
         }
         }
    }


    on_rez(integer start_param)
        llSetText(ownerName
    {
                    +"\ndonations so far: " + (string)totalDonations + " L$"
        llResetScript();  
                    + "\nlast donation: " + (string)lastDonation + " L$ (" + lastDonor + ")"
                    + "\ntop donation: " + (string)topDonation  + " L$ (" + topDonor + ")",
                        colorToUse, floattextAlpha);
     }
     }
}</lsl>
}
</lsl>

Revision as of 11:09, 19 October 2012

  • Put this script in any object you own for an instant tipjar.
  • By Toady Nakamura

<lsl> integer totaldonated;

default {

   on_rez( integer sparam )
   {
       llResetScript(); // reset when rezzed out
   }
   state_entry()
   {
       llSetText("Donation Box\nL$0 Donated so far",<1.0,1.0,1.0>,0.85);
       // set text, "message", color vector (here white), and intensity (here 85%)
   }
   money(key id, integer amount)// When someone pays the system records their key & the amount
   {
       totaldonated = totaldonated + amount;
       // add the amount just given to the total from before
       // "amount" comes from the money event
       llSetText("Donation Box \n L$" + (string)totaldonated + " Donated so far",<1.0,1.0,1.0>,0.85);
       // set the text to show the gift
       llInstantMessage(id,"Thank you for the donation!, " + llKey2Name(id));
       // send a private thank you to giver.
       // "id" comes from the money event
       llInstantMessage(llGetOwner(), llKey2Name(id) + " just donated $L" + (string)amount);
       // privately message owner to say how much the donation was and the name of the giver.
   }

} </lsl>

Fancier Recording Tipjar

I found this one on the LSL-101/Logic page and added it here for reference so you can see the difference between a simple script and one which is a bit more complicated. This one shows

  • Donations so far
  • Last donation
  • Largest donation

<lsl> 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);
   //  PUBLIC_CHANNEL has the integer value 0
       llSay(PUBLIC_CHANNEL,
           "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);
   }

} </lsl>