Variables Needed for the Door
Several variables are defined for the smart door. Some of the variables are the same as were used for the open door shown in Recipe 3.2. The new variable is shown here.
list allow;
The allow variable keeps a list of who is allowed to open the door. This Recipe only describes what was added to Recipe 3.2 to create the smart door. This includes the security features, but not the mechanics of how to open the door. For more information on the mechanics of opening the door, refer to Recipe 3.2.
The functionality for this Recipe is split among two event handlers. The touch_start event handler allows the door to be opened by touching it. The listen event handler allows the owner of the door to add users to the list of people who are allowed to open the door.
Controlling Access to the Door
All of the door security is handled by the touch_start event handler. First, the start_touch event handler checks to see who touched the door. If the owner of the door touched the door, the door should always be opened.
touch_start(integer total_number)
{
key who = llDetectedKey(0);
integer shouldOpen = 0;
if( who==llGetOwner() )
shouldOpen = 1;Next, the name of the touching user is converted to be entirely upper case. The list of allowed users is checked. If the user is found, the door will be opened. A Linden Scripting Language list can easily be searched by calling llListFindList.
string name = llToUpper(llDetectedName(0)); if( llListFindList(allow,[name]) != -1 ) shouldOpen = 1;
If it has been determined that the door should be opened, the door says hello to the touching user. The door then opens.
if( shouldOpen == 1 )
{
llSay(0,"Hello " + llDetectedName(0) );
door(DOOR_OPEN);
state open_state;
}If the door should not be opened, play a doorbell sound and say who is at the door.
else
{
llSay(0,llDetectedName(0) + " is at the door." );
llTriggerSound("doorbell", 0.8);
}
}To determine whether users should be allowed or not, the touch_start event handler requires that all authorized users be added to the list. Users are added to the list by talking to the door. This is covered in the next section.
Listening for Commands
The listen event handler uses the pop function that was introduced in Recipe 2.3. For more information about how the pop function works, refer to Recipe 2.3. The pop function is used to break open the commands to the door.
The listen event handler begins by making sure that the one talking to it is the owner of the object. If the one talking is not the owner, the conversation is ignored.
listen(integer channel, string name, key id, string message)
{
if( id==llGetOwner() )
{
text = message;
string prefix = llToLower(pop());The first space delimited word is extracted from what was said to the door. This string is converted into lower case. The script checks to see whether this word was door. Since all the commands to the door start with the word “door”, if the prefix is not door, the door will do nothing further.
if( prefix=="door" )
{Once it has been established that what the owner said is prefixed by door, the next space delimited word is the command. The command is obtained. If there is no command, the door simply identifies itself.
string command = pop();
if( command=="" )
{
llSay(0,"I am the smart door!");
}If the command is clear, the list is set to an empty array, which clears it.
else if( command=="clear" )
{
llSay(0,"Clearing access list.");
allow = [];
}If the command is add, the door obtains the rest of the text and adds that user. It is important to not use pop at this point. User names in Second Life are a first name, a space and the last name. If pop is used, only the first name would be obtained.
else if( command=="add" )
{
if( llStringLength(text)> 0 )
{
text = llToUpper(text);
allow+=[text];
llSay(0,"Adding " + text );
}If no user is specified, display an error.
else
{
llSay(0,"You must also specify an avatar when using add.");
}
}If the command list is given, the length of the list is obtained. If the list has a length equal to zero, the door reports that it can only opened by its owner.
else if( command=="list" )
{
integer length = llGetListLength(allow);
if( length==0 )
{
llSay(0,"No one, other than my owner, may open me.");
}Otherwise, a for loop is used to display the entire list of allowed users.
else
{
integer i;
llSay(0,"The following people have access to open me:");
for (i = 0; i < length; ++i)
{
llSay(0,llList2String(allow, i));
}
}
}If the command does not match any of the previous values, the door states that it did not understand the command.
else
{
llSay(0,"I did not understand that command.");
}
}
}
}The rest of the door recipe is the same as Recipe 3.2. To see the mechanics of opening and closing the door, Refer to Recipe 3.2.




