Difference between revisions of "User:Toady Nakamura/Owner Only"

From Second Life Wiki
Jump to navigation Jump to search
(new page for owner only conditionals... will add more)
 
m (added simpler example & more complicated one, prettier format)
 
Line 3: Line 3:
Filter the key of the toucher against the owner's key by using the conditional format (if *what I got* == *the limiting factor*).
Filter the key of the toucher against the owner's key by using the conditional format (if *what I got* == *the limiting factor*).


This example of how to do it shows that if toucher is owner, make box green, else make box red!
== Example 1 - Congratulate owner ==
 
The following example responds to owner's touch but does nothing at all if anyone else touches.
if toucher key exactly equals owner's key - congratulate owner!


<source lang="lsl2">
<source lang="lsl2">
default
{   
    touch_start(integer num_detected)
    {
        if(llDetectedKey(0) == llGetOwner())
        {
          llRegionSayTo(owner, 0, "You alone can touch me!");
        }
      // the else nothing happens doesn't need to be defined
        }
    }
}
</source>


== Example 2 - If owner / else not owner ==
This example shows how to define the owner's key, and provides an if/else conditional structure:
* if toucher is owner, make box green, else make box red!
<source lang="lsl2">
key owner;
key owner;


Line 20: Line 41:
         if(llDetectedKey(0) == owner)
         if(llDetectedKey(0) == owner)
         {
         {
           llSetColor(<0,1,0>, ALL_SIDES);
           llSetColor(<0,1,0>, ALL_SIDES); // green
         }
         }
         else
         else
         {
         {  
           llSetColor(<1,0,0>, ALL_SIDES);
           llSetColor(<1,0,0>, ALL_SIDES); // red
         }
         }
     }
     }
Line 36: Line 57:
     }
     }
}
}
</source>
</source>
*changed event
You need this event so if the item changes to new owner, it resets and gets the new owner's UUIDkey... otherwise it stays fixated on the first one it ever knew as its owner


== Example 3 - If owner or friend/ else if other friend/ else none of the above ==


This example shows one way to code different behaviors for different keys.


<source lang="lsl2">
key owner;
key friend_key = "000000000000000000000"; // use a real key here!!
key friend2_key ="9999999999999999999999";// use a real key here!!


default
{
    state_entry()
    {
        owner = llGetOwner();
    }
   
    touch_start(integer num_detected)
    {
key toucher = llDetectedKey(0);
        if(toucher == owner || toucher == friend_key) // either you or first friend
        {
          llSetColor(<0,1,0>, ALL_SIDES); // green
        }
else if(toucher == friend2_key); // if second friend
{
    llSetColor(<1,1,0>, ALL_SIDES); // yellow
}
        else // anybody else
        {
          llSetColor(<1,0,0>, ALL_SIDES); // red
        }
    }


    changed(integer change)
    {   
        if (change & CHANGED_OWNER)
        {
            llResetScript();
        }
    }
}
</source>


==* Why two == or two || ? ==
Because the if/else comparison is only TRUE/FALSE, the [[LSL_Operators Math Operators (symbols)]] are [[Boolean]].  Boolean Logic works on three Boolean Operators: “Or,” “And,” and “Not”. Using those three, all results of Boolean Logic resolve as either TRUE (1) or FALSE (0).  You can compare positively as we do here, or compare to a negative by using ''if (toucher_key != owner)'' format.
==* Why do two examples have the [[Changed|changed event]]?==
'''Example 1 does not have changed event''' because script checks for owner after every touch. Which works but is not ideal.


'''Examples 2 & 3 do have changed events''' because script checks once in state_entry and stores the answer.
If the item changes to new owner, it resets and gets the new owner's UUIDkey.


Visit my LSL wiki page for my library of simple scripts ! Toady Nakamura
There are other "for owner only" tricks!  See the simple listener example!
---------------------------------------------------------------------------
Visit my LSL wiki page for my library of simple scripts ! [[User:Toady Nakamura|Toady Nakamura]]

Latest revision as of 16:17, 5 March 2021

If you want your object to only react to the owner, you need to get and use the owner's UUID key.

Filter the key of the toucher against the owner's key by using the conditional format (if *what I got* == *the limiting factor*).

Example 1 - Congratulate owner

The following example responds to owner's touch but does nothing at all if anyone else touches. if toucher key exactly equals owner's key - congratulate owner!

default
{    
    touch_start(integer num_detected)
    {
        if(llDetectedKey(0) == llGetOwner())
        {
           llRegionSayTo(owner, 0, "You alone can touch me!");
        }
       // the else nothing happens doesn't need to be defined
        }
    }
}

Example 2 - If owner / else not owner

This example shows how to define the owner's key, and provides an if/else conditional structure:

  • if toucher is owner, make box green, else make box red!
key owner;

default
{
    state_entry()
    {
        owner = llGetOwner();
    }
    
    touch_start(integer num_detected)
    {
        if(llDetectedKey(0) == owner)
        {
           llSetColor(<0,1,0>, ALL_SIDES); // green
        }
        else
        { 
           llSetColor(<1,0,0>, ALL_SIDES); // red
        }
    }

    changed(integer change)
    {    
        if (change & CHANGED_OWNER)
        {
            llResetScript();
        }
    }
}

Example 3 - If owner or friend/ else if other friend/ else none of the above

This example shows one way to code different behaviors for different keys.

key owner;
key friend_key = "000000000000000000000"; // use a real key here!!
key friend2_key ="9999999999999999999999";// use a real key here!!

default
{
    state_entry()
    {
        owner = llGetOwner();
    }
    
    touch_start(integer num_detected)
    {
	key toucher = llDetectedKey(0);

        if(toucher == owner || toucher == friend_key) // either you or first friend
        {
           llSetColor(<0,1,0>, ALL_SIDES); // green
        }

	else if(toucher == friend2_key); // if second friend
	{
	    llSetColor(<1,1,0>, ALL_SIDES); // yellow
	}
        else // anybody else
        {
           llSetColor(<1,0,0>, ALL_SIDES); // red
        }
    }

    changed(integer change)
    {    
        if (change & CHANGED_OWNER)
        {
            llResetScript();
        }
    }
}

* Why two == or two || ?

Because the if/else comparison is only TRUE/FALSE, the LSL_Operators Math Operators (symbols) are Boolean. Boolean Logic works on three Boolean Operators: “Or,” “And,” and “Not”. Using those three, all results of Boolean Logic resolve as either TRUE (1) or FALSE (0). You can compare positively as we do here, or compare to a negative by using if (toucher_key != owner) format.

* Why do two examples have the changed event?

Example 1 does not have changed event because script checks for owner after every touch. Which works but is not ideal.

Examples 2 & 3 do have changed events because script checks once in state_entry and stores the answer. If the item changes to new owner, it resets and gets the new owner's UUIDkey.

There are other "for owner only" tricks! See the simple listener example!


Visit my LSL wiki page for my library of simple scripts ! Toady Nakamura