Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Scripting 103 Class Project - Simple Networked TV System

Hank Ramos
Lifetime Scripter
Join date: 15 Nov 2003
Posts: 2,328
08-09-2005 05:55
_______________________
Class Title: LSL Scripting 103
Length: 1 hour+ (45-55 minutes instruction, 10 minutes question, 5 minutes slack)
Description: Found scripting to be your thing in SL and want even more advanced information? This class will cover advanced topics in LSL scripting such as Sending and Receiving Email, Reading Notecards (dataserver event), Changed Event, FOR loops, and Asynchronous Code.

Student Goals:
1. Understand asynchronous code in scripts. (i.e. email and notecard reading)
2. Understand FOR loops.
3. Understand available I/O options between non-linked prims (primarily inter-sim communication).
4. Understand using states
5. Make your own user function.

Note: this class will involve the dissection of a notecard distribution and/or simple TV network system.
_______________________________________________________________
TV Network System
This example will show how to make a world-wide Television (Image Slide-Show) system where you can have several TV Displays showing programs read from notecards on a central "server" prim.

Steps to Use...
1. Rez both the TV Set and TV Station
2. Touch the TV Station prim to have it chat the TV Station Prim's Key.
3. Open the "TV Script" found in the TV Set prim. Modify the TV Station Prim key in that script with the key from your TV Station.
4. Drag-And-Drop one of the Program notecards to the TV Station.
5. Touch the TV Set to turn it on (if it is already off, black color).
6. Watch the show! :)

_______________________________________________________________
Note: this is not intended to be a complete, full-featured, useable TV Network System. It is a simplified implementation to illustrate the LSL concepts listed above for a 1.5 hour scripting class. Feel free to use it as a basis for other projects, or to start coding your own world-wide system of any type!

You can obtain the completed project and these instructions at my Script Shop in Grignano (100,100) or you can create two prims and two notecards, and then stuff the following scripts in each...
_______________________________________________________________
TV Station Script
CODE
//TV Station Script
//This script keeps track of the
//registered TV sets and keeps
//them updated with the lastest
//program notecard.
list teeVees;
key programID = NULL_KEY;

updateTV(integer number)
{

if (programID != NULL_KEY)
{
string TVUUID;

//Get UUID of TV From our List
TVUUID = llList2String(teeVees, number);

llOwnerSay("Sending Notcard: " + (string)programID);

//Send the UUID of the program notecard.
//The TV set will read the notecard.
//Note: Scripts are delayed 20 seconds for each email
llEmail(TVUUID + "@lsl.secondlife.com", (string)programID, "");
}
}
default
{
state_entry()
{
//Set the text at the top of the prim
llSetText(llGetObjectName(), <1,1,1>, 1);

//Periodically check for emails from TVs wishing to register
llSetTimerEvent(10);
}

on_rez(integer startup_param)
{
llResetScript();
}

touch_start(integer num_detected)
{
integer x;

for (x = 0; x < num_detected; x++)
{
//Only process touches by the owner of this prim
if (llDetectedKey(x) == llGetOwner())
{
//Chat our key so you can update the TV
//script with the appropriate key to send
//registrations
llOwnerSay((string)llGetKey());
}
}
}
timer()
{
llGetNextEmail("", "");
}
email(string time, string address, string subj, string message, integer num_left)
{
//Only register if the new TV isn't
//in the list of TVs
if (llListFindList(teeVees, [subj]) == -1)
{
llOwnerSay("Registering TV: " + (string)subj);
teeVees += subj; //Add TV to List
}
//Update the TV with the notecard
updateTV(llGetListLength(teeVees) - 1);
}

changed(integer change)
{
integer x;

//If the inventory of the TV Station has changed
if (change & CHANGED_INVENTORY)
{
string notecardName;

//Get Notecard information
notecardName = llGetInventoryName(INVENTORY_NOTECARD, 0);
programID = llGetInventoryKey(notecardName);

llOwnerSay("Processing Notecard: " + notecardName);

//Delete Notecard from TV Station's Contents.
//Note: the notecard still exists in the SL DataServer
//because we have a copy of it. Copies of notecards
//all have the same UUID key
llRemoveInventory(notecardName);

//Loop through all the registered TVs
//and update them with the UUID of the new program notecard
for (x = 0; x < llGetListLength(teeVees); x++)
{
//Update this TV in the list
updateTV(x);
}
}
}
}


TV Set Script
CODE
//TV Set Script
//This script receives the UUID
//of a notecard, which contains
//a list of textures to display
//in sequence.
key TVStationKey = "Copy-Paste TV Station Prim's Key Here";
key notecardID = NULL_KEY;
key requestID;
integer lineNum;
list textureKeys;

default
{
state_entry()
{
//Set the text at the top of the prim
llSetText(llGetObjectName() + " initializing...", <1,1,1>, 1);

//Set the color of the prim to white
llSetColor(<1,1,1>, ALL_SIDES);

//Remove any currently displayed textures and replace with "blank"
llSetTexture("8b5fec65-8d8d-9dc5-cda8-8fdf2716e361", ALL_SIDES);

//Register ourselves with the TV Station
llEmail((string)TVStationKey + "@lsl.secondlife.com", (string)llGetKey(), "");

state idle;
}
}

state idle
{
state_entry()
{
//Set the text at the top of the prim
llSetText(llGetObjectName() + " Idle", <1,1,1>, 1);

//Set the color of the prim to black
llSetColor(<0,0,0>, ALL_SIDES);

llPlaySound("e1fa0145-69fb-dc75-645c-a2553407873c", 1.0);
}
touch_start(integer num_detected)
{
integer x;

for (x = 0; x < num_detected; x++)
{
//Only process touches by the owner of this prim
if (llDetectedKey(x) == llGetOwner())
{
state running;
}
}
}
}

state running
{
state_entry()
{
//Set the text at the top of the prim
llSetText(llGetObjectName() + " Running", <1,1,1>, 1);

//Set the color of the prim to white
llSetColor(<1,1,1>, ALL_SIDES);

//Configure Timer to periodically checking for emails
llSetTimerEvent(10);

lineNum = 0;
llPlaySound("859a4072-f5c5-0dc9-24df-760174714aa5", 1.0);
}

on_rez(integer start_params)
{
llResetScript();
}

touch_start(integer num_detected)
{
integer x;

for (x = 0; x < num_detected; x++)
{
//Only process touches by the owner of this prim
if (llDetectedKey(x) == llGetOwner())
{
state idle;
}
}
}

timer()
{
//Check for email
llGetNextEmail("", "");

//Retrieve the texture key from the list and set the texture on the TV screen.
if (llGetListLength(textureKeys) > 0)
{
llSetTexture(llList2Key(textureKeys, lineNum), ALL_SIDES);
}

lineNum++; //Increase to next texture key

//Don't display more keys than we have in the list.
if (lineNum >= llGetListLength(textureKeys))
{
lineNum = 0;
};
}

email(string time, string address, string sub, string message, integer num_left)
{
//Get the Notecard ID
llOwnerSay("Receiving Notecard: " + sub);
notecardID = (key)sub;

state reading;
}
state_exit()
{
//Timers are funny. Disable them when leaving a state,
//otherwise they will fire unexpectantly when we switch
//back to this state.
llSetTimerEvent(0);
}
}

state reading
{
state_entry()
{
lineNum = 0;
textureKeys = [];
//Read the first line of the notecard
//We will read subsequent lines after we receive the one
//we just requested. Stuff each line in to the textureKeys list.
requestID = llGetNotecardLine(notecardID, lineNum);

//Set the text at the top of the prim
llSetText("Reading Notecard Line #" + (string)lineNum, <1,1,1>, 1);
}

on_rez(integer start_params)
{
llResetScript();
}

dataserver(key requested, string data)
{
//Make sure this request was the one we made
if (requested == requestID)
{
//Check to see if we are at the end of the notecard
if (data == EOF)
{
//We hit the end of the file!
//Switch to the running state
state running;
}
else
{
//We successfully read the line
//Set the texture using the UUID specified
if (data != "")
{
textureKeys += (key)data;
}
lineNum += 1;

//Recursively request the next line
requestID = llGetNotecardLine(notecardID, lineNum);

//Set the text at the top of the prim
llSetText("Reading Notecard Line #" + (string)lineNum, <1,1,1>, 1);
}
}
}
}


Program Notecard #1
CODE
563a21fd-4012-52b2-6615-e24c38ab26a5
ac0ac92d-db23-566f-5a26-7be29403fe40
9c68d1f4-b9e6-7d58-f2b7-612adcd6a44a
f1c53ad4-65bb-2ecd-716e-254d77cd7eb3


Program Notecard #2

CODE
42754bee-4638-cf11-743f-c8b8f9871633
8f6cece1-37a3-50f3-0e81-4d59075a4fcc
92609fbe-a5b3-e33a-34eb-f05b1d071bc8
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
08-09-2005 16:10
/54/98/57084/1.html
_____________________
i've got nothing. ;)