Advertisement
Gayngel

Demo item script - Detach after elapsed time

May 22nd, 2023 (edited)
1,779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A script to put in demo worn items to allow the purchaser a certain amount of time to try on the item. Once the time has expired it will detach and they can not wear it again.
  2.  
  3. // make this script no mod so it can not be reset by the wearer.
  4.  
  5. // can work for copy or no copy items
  6.  
  7. // unless the user is going to copy and reattach this item a million times, this is a sufficient way to allow a user only a certain amount of time to view the worn demo.
  8.  
  9.  
  10.  
  11. key creator = ""; // the uuid key of the creator, put it between the brackets. Used so that item wont detach and delete if the wearer is the creator.
  12.  
  13. float time = 300.0; // time in seconds to allow the user to wear the demo. 300 seconds is 5 minutes.
  14.  
  15. float elapsed_time;
  16.  
  17. integer allow_timer = TRUE; // a boolean to make sure the timer does not restart after repeatedly attaching.
  18.  
  19. integer allow_wearing = TRUE; // a boolean to allow the demo to be worn, if the boolean is false don't allow the item to be worn.
  20.  
  21. integer tick;
  22.  
  23.  
  24. string FormatFloat(float f, integer num_decimals) // a function to cut trailing zeros from a float. Taken from this wiki: https://wiki.secondlife.com/wiki/User:Pedro_Oval/Float_formatting_functions
  25. {
  26.     float rounding = (float)(".5e-" + (string)num_decimals) - 0.0000005;
  27.     if (f < 0.0) f -= rounding; else f += rounding;
  28.     string ret = llGetSubString((string)f, 0, num_decimals - 7);
  29.     if ((float)ret == 0. && llGetSubString(ret, 0, 0) == "-")
  30.         ret = llDeleteSubString(ret, 0, 0);
  31.     if (! num_decimals) ret += "0";
  32.     return ret;
  33. }
  34.  
  35. default
  36. {
  37.  
  38.    state_entry()
  39.     {
  40.        //  creator = llGetCreator(); // optional way to get the creator key. uncomment this line if you want to get the key of the creator this way
  41.         tick = 0;
  42.         elapsed_time = time - (float)tick;
  43.        
  44.     }
  45.  
  46.     on_rez(integer start_param)
  47.     {
  48.        
  49.      integer at = llGetAttached(); // check if item is attached.
  50.      
  51.      if(at == 0) // if item is not attached either delete the item it is copy or stop the timer
  52.      {
  53.          llDie(); // if item is copy we can just delete it
  54.          
  55.          llOwnerSay("Please wear this item. Don't rez it.");
  56.          
  57.          
  58.          // option for no copy items below. // comment out the llDie() above if item is no copy and uncomment the below lines.
  59.         // you could also keep llDie() and delete the item if it is a free demo.
  60.        
  61.         // llSetTimerEvent(0.0);
  62.         // allow_timer = TRUE;
  63.          
  64.          
  65.        
  66.      }  
  67.        
  68.        
  69.     }
  70.    
  71.     attach(key id)
  72.     {
  73.      
  74.      if(id)
  75.      {
  76.        
  77.        if(id != creator)
  78.        {
  79.            if(allow_timer == TRUE)
  80.            {
  81.            allow_timer = FALSE;    
  82.            llSetTimerEvent(1.0);
  83.          
  84.            }
  85.            
  86.            if(allow_wearing == TRUE)
  87.            {
  88.                 float elapsed = elapsed_time/60;
  89.      
  90.              llOwnerSay("You have " + FormatFloat(elapsed,2) + " minutes to wear this demo item");  
  91.                
  92.            }
  93.            
  94.            else
  95.            {
  96.               llOwnerSay("The time to view this item has expired.");  
  97.              llRequestPermissions(llGetOwner(),PERMISSION_ATTACH);  
  98.                
  99.            }
  100.            
  101.            
  102.        }  
  103.          
  104.          
  105.      }  
  106.        
  107.     }
  108.    
  109.    
  110.    
  111.  
  112.    
  113.    
  114.    
  115.     timer()
  116.     {
  117.        
  118.       ++tick;
  119.      
  120.       elapsed_time = time - (float)tick; // get elapsed time
  121.      
  122.        if(elapsed_time <= 0.0)
  123.        {
  124.            llSetTimerEvent(0.0);
  125.            llOwnerSay("The time to view this item has expired.");
  126.            allow_wearing = FALSE; // if time has expired dont allow item to beworn again.
  127.    
  128.            llRequestPermissions(llGetOwner(),PERMISSION_ATTACH);
  129.            
  130.            
  131.        }
  132.        
  133.        
  134.     }
  135.    
  136.     run_time_permissions(integer perm)
  137.     {
  138.         if(perm & PERMISSION_ATTACH)
  139.          llDetachFromAvatar();  //detach from avatar after elapsed time
  140.        
  141.        
  142.     }
  143.    
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement