Difference between revisions of "Category:LSL Key"

From Second Life Wiki
Jump to navigation Jump to search
(Added caveat about key literals.)
(^_^)
Line 23: Line 23:
When a key is supplied to you as a text string, you convert it to the key data type like this:
When a key is supplied to you as a text string, you convert it to the key data type like this:


(key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2";
<lsl>(key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2";</lsl>
 
Implicit conversion will happen automatically when supplying a string where a key is required.
 
<lsl>key uuid = "a822ff2b-ff02-461d-b45d-dcd10a2de0c2";
llKey2Name("a822ff2b-ff02-461d-b45d-dcd10a2de0c2");</lsl>
 
However there is no implicit conversion with [[llListFindList]]. llListFindList requires not only the values to match but also the types.
 
<lsl>llListFindList(["a822ff2b-ff02-461d-b45d-dcd10a2de0c2"], [(key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2"]) == -1;</lsl>




Line 46: Line 55:
To test for a valid key, just do this:
To test for a valid key, just do this:


<pre>if(uuid){
<lsl>if(uuid){
      //do something
    //do something
      }
}</lsl>
</pre>


if(uuid) will only return true if it is supplied a key that is both (A) valid, and (B) NOT a NULL_KEY.
<code>if(uuid)</code> will only return true if it is supplied a key that is both (A) valid, and (B) NOT a [[NULL_KEY]].


Tip! In techy talk, this method is called "passing it as the parameter for a {{LSLGC|Conditional|conditional}}"
Tip! In techy talk, this method is called "passing it as the parameter for a {{LSLGC|Conditional|conditional}}"
Note! It is important for the above example that <code>uuid</code> be defined as a key. It can of course be typecast to be a key as well: <code>if((key)uuid)</code>


Here is an example of how to build a function around this:
Here is an example of how to build a function around this:
Line 67: Line 77:


* <code>if (uuid)</code> is a special case. Keys cannot be converted to integers, so logical operators such as <code>!</code>, <code>||</code> and <code>&&</code> cannot be used with keys. If you wish to do <code>if (!uuid) // Some action</code> then you can use a simple workaround like so: <code>if (uuid) {} else // Some action</code>. This is particularly useful if we wish to discard invalid string content (i.e - ensure that a key is actually a key) like so:<lsl>if (uuid) {} else if (uuid != NULL_KEY) uuid = NULL_KEY;</lsl>
* <code>if (uuid)</code> is a special case. Keys cannot be converted to integers, so logical operators such as <code>!</code>, <code>||</code> and <code>&&</code> cannot be used with keys. If you wish to do <code>if (!uuid) // Some action</code> then you can use a simple workaround like so: <code>if (uuid) {} else // Some action</code>. This is particularly useful if we wish to discard invalid string content (i.e - ensure that a key is actually a key) like so:<lsl>if (uuid) {} else if (uuid != NULL_KEY) uuid = NULL_KEY;</lsl>
* A list of key literals will actually be a list of string literals, thus llListFindList will always fail if you try to search for actual keys in that list.
* Be careful when adding key literals to lists, no implicit typecasting will take place. Failing to ensure that key literals are keys will cause problems with [[llListFindList]].
 


== Extended Key Operations ==
== Extended Key Operations ==

Revision as of 22:05, 5 June 2011

A key is a unique identifier in Second Life for anything mostly, be it a prim, avatar, texture, etc.

You may see key referred to as UUID, UID, "Asset UUID", or "asset-ID."

The key itself is formed of hexidecimal characters (a-f and 0-9) and each section of the key is broken up by dashes.

An example key:

"a822ff2b-ff02-461d-b45d-dcd10a2de0c2"


Getting a Key

There are several ways to acquire the key of something:
1) Having someone supply it to you;
2) Using a native LSL function such as llGetKey, etc.
3) In your inventory, right-clicking over something and choosing "Copy Asset UUID." Note this will only work on items that you have full permissions to.


Converting Keys

When a key is supplied to you as a text string, you convert it to the key data type like this:

<lsl>(key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2";</lsl>

Implicit conversion will happen automatically when supplying a string where a key is required.

<lsl>key uuid = "a822ff2b-ff02-461d-b45d-dcd10a2de0c2"; llKey2Name("a822ff2b-ff02-461d-b45d-dcd10a2de0c2");</lsl>

However there is no implicit conversion with llListFindList. llListFindList requires not only the values to match but also the types.

<lsl>llListFindList(["a822ff2b-ff02-461d-b45d-dcd10a2de0c2"], [(key)"a822ff2b-ff02-461d-b45d-dcd10a2de0c2"]) == -1;</lsl>


Displaying avatar or group information

If you know an avatar's key, you can display the avatar's name in the viewer window and chat history by using the following special URL: <lsl>llOwnerSay("secondlife:///app/agent/" + (string)owner_key + "/about");</lsl>

This displays both display name and username as a clickable link that brings up an avatar profile window when clicked. It is easier than using llRequestAgentData or llRequestUsername or llRequestDisplayName, since there is no need to use a dataserver event.

If you know a group key (as a result of calling llGetObjectDetails with OBJECT_GROUP, or calling llGetParcelDetails with PARCEL_DETAILS_GROUP), you can display the group's name with the following special URL: <lsl>llOwnerSay("secondlife:///app/group/" + (string)group_key + "/about");</lsl>

This displays the group name as a clickable link that brings up a group profile window when clicked. This is especially useful since there is no other way to do this; there is no LSL function to print out a group's name.

Finally, if you know a parcel key (as a result of calling llGetParcelDetails with PARCEL_DETAILS_ID), you can create a clickable link that brings up a place profile window when clicked, using the following special URL: <lsl>llOwnerSay("secondlife:///app/parcel/" + (string)parcel_key + "/about");</lsl>


Testing for a valid key

To test for a valid key, just do this:

<lsl>if(uuid){

    //do something

}</lsl>

if(uuid) will only return true if it is supplied a key that is both (A) valid, and (B) NOT a NULL_KEY.

Tip! In techy talk, this method is called "passing it as the parameter for a conditional"

Note! It is important for the above example that uuid be defined as a key. It can of course be typecast to be a key as well: if((key)uuid)

Here is an example of how to build a function around this:

<lsl> integer isKey(key in) {

   if(in) return 2;
   return (in == NULL_KEY);

} </lsl>

Caveats

  • if (uuid) is a special case. Keys cannot be converted to integers, so logical operators such as !, || and && cannot be used with keys. If you wish to do if (!uuid) // Some action then you can use a simple workaround like so: if (uuid) {} else // Some action. This is particularly useful if we wish to discard invalid string content (i.e - ensure that a key is actually a key) like so:<lsl>if (uuid) {} else if (uuid != NULL_KEY) uuid = NULL_KEY;</lsl>
  • Be careful when adding key literals to lists, no implicit typecasting will take place. Failing to ensure that key literals are keys will cause problems with llListFindList.

Extended Key Operations

These functions have been created and contributed by LSL users to perform operations not covered by built-in LSL functions.


function purpose
GenerateKey Generates an MD5-based (version 3) type UUID. Useful for identifying link-messages and for other purposes.
GenUUID Generates a UUID based on PHP com_create_guid.