Tuesday 7 June 2011

Second Life - Emails,web pages and Notecard givers

Introduction

Linden Scripting, enables residents to make use of existing web technologies. And amongst others, residents can create objects that communicate with the Internet, display web pages and send emails. In addition, residents can embed information in Notecards and share these with other residents.
Using some examples, in this blog I will describe how to send an email, launch a web page and give a Notecard to an avatar using Linden Scripting.

Emails and Web pages

The aim of this task is to create an object that can send an email and launch an Internet web page. To achieve this, using some prims, I created a chair that when touched a web page is launched in the client Internet browser. In addition,when an avatar sits on the chair, an email is send to my Gmail mailbox.
To start with, I created the chair and ensured that avatars can sit correctly without using scripting.
In Linden Scripting, the llLoadURL() function is used to launch a web page in an external Internet browser. This function is used as follows:
llLoadURL(key avatar, string message, string url);
To instruct the chair to launch a web page when touched, I used the following script:
The llDetectedKey(0) function in line 12, returns the UUID key of the avatar and stores the value in a variable. Line 13, launches a Wikipedia page and displays the message “About chairs”.
When the chair is touched, the Second Live Viewer will launch the following dialog:
In Linden Scripting to send an email, the llEmail() function is used as follows:
llEmail(string email address, string subject, string message);
Sending an email is not difficult, the tricky part in this task is how to send an email when an avatar sits on the chair.
The solution is simple. Avatars and prims are all objects of the same kind, when an avatar sits on a target prim, the avatar is linked to the target prim. So to make it simpler, if I count the number of prims when the chair is empty, the result will be 4 (the chair is made up of 4 objects). If I count the number of prims when someone is sitting on my chair, the result will be 5 (4 objects plus the avatar).
To send the email, I used the changed() event handler. Amongst others, this event is triggered when the number of prims changes. The event handler change parameter is a bit-field, and contains information on what type of change has been performed. Amongst others this bit-field can indicate when:
  • An object is added, removed from your inventory
  • An object change color
  • The number of linked objects change
For a full list, refer to changed event.
The email address of the recipient is stored in a variable in line 18. The if statement in line 21 checks if the bit-field is CHANGED_LINK. In line 23, llGetNumberOfPrims() is used to get the total number of prims.
If the total number of prims is 5, then an avatar has sit on the chair; script lines 27 and 28 are executed.
In line 27, the last linked object name within the group (the avatar) is stored in a variable using the llGetLinkName(integer index); this will be used in the email body. The email is send in line 28, and the email is as follows:
Note, the email header contains information on the Region, and the region Local-Position. This enables the recipient to identify from which area the object is sending the email.

Notecards and Givers

In Second Life, Notecards are inventory items that contain plain text or embedded items. A typical example when such notes are used is when land owners wish to provide visitors with further information related to their land.
The aim of this task is to create a Notecard giver.
To create a new Notecard and embed the Notecard to an object:
1. From My Inventory, right click the Notecards folder and select New Notecard. A new Notecard is added in the Notecards folder.
2. Provide a name for the new Notecard and fill in the description and the contents. For example, my Notecard looks something like the following:
3. Create an object and add a new script. For this task, I added a simple bench where an avatar can sit.
4. Open the object building tool bar and select the Content tab. Drag and drop the Notecard into the contents folder. Your building tool bar should look as follows:
In this example, the Notecard is given when an avatar touches the bench. To achieve this, I added the following code to the default state:
The llGetInventoryName() is used to retrieve the Notecard name. Every inventory object has an index, having the first object with an index of 0. Line 9 is retrieving the name of the first Notecard from the inventory. Line 10, gets the avatar key, this will be used as our Notecard recipient.
In line 13, using llGiveInventory(key avatar, string object name), the Notecard is given to the detected avatar.
When the bench is clicked/touched, the following dialog will notify the avatar that a Notecard is being offered and enables the user to Keep/Decline or Block the Notecard.
When accepting a Notecard, the Notecard is added to your Notecard folder in the MyInventory section.

Conclusion

While researching for the right functions to use, I bumped into some forums that stated that in Second Life, sending emails are usually used as sensors. This means that object owners use email technologies to be notified when their objects have been touched or changed (similar to an alarm).
On the other hand, Notecards and external web pages are usually used to provide more information to a visitor/residents.
Happy Teleporting...

No comments:

Post a Comment