Second Life Simple Data Storage

April 24, 2008 · Filed Under Second Life · Comment 

In creating scripts for Second Life, I’ve tried to keep the scripts as portable and re-usable as possible. At the most basic level, this means creating a separation of code and data. For scripts that are used in a limited number of prims, this can be as simple as setting customizable variables at the top of the script. For scripts that are going to be used in many prims, this system requires maintaining a unique copy of each script in each prim. If your base script is updated to fix a bug or add a feature, you’ll need to update each script by hand or overwrite the script in prim and reset the custom variables. Both options are prone to error.

For scripts used in many prims, storing the data for those variables in the prim’s description field can be an effective solution. This allows you to replace the script without impacting the custom data in each prim. The description field is limited to 127 characters, so this method is only useful with short length data. Also, since the description field can be publicly viewed in prim properties, this method should not be used to store private or sensitive data. On the plus side, this data can be updated dynamically using llSetObjectDesc().

When storing the value of a single variable in the description field, use this code to retrieve the value. If the type is anything other than a string, simply prefix the function with the data type. In this case, the variable type is a vector.

vector myVector; //declare variable type
myVector = (vector)llGetObjectDesc(); //read the description, a string by default, and convert to a vector

If you need to store multiple values, format the data in the description field as a list, such as “Someplace;<72,89,25>”, read the data and parse it. This example uses the semi-colon as a delimiter.

list description;
string locationName;
vector locationTarget;

description = llParseString2List(llGetObjectDesc(), [";"], [""] );
locationName = llList2String(description, 0);
locationTarget = (vector)llList2String(description, 1);

I have found this method to be very effective and efficient for scripts that contain a limited amount of custom data. To store larger amounts of data, you’ll need to use a notecard or data store external to the prim.