Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Help with a notecard reading script

Over Sleeper
I Dream in LSL
Join date: 12 Jan 2006
Posts: 141
03-13-2006 12:55
I am trying to make it so I have a notecard with commands and responses. When I say a command then the script executes whatever the notecard says like this:

CODE
red,Roses are red!
blue,Violets are blue!
green,Grass is green!



So now if I type in /1 blue, I will get the response "Violets are blue!"

I have tried at the code for some time now taken from some examples I see here in the forums and the wiki. But I could use some help, I am doing something wrong:

CODE

integer input = 1; // channel to listen to
list myList = [];
integer lineNum = 0;


default
{
dataserver(key id, string data)
{
if (data != EOF) // Not EOF, so we're still reading lines
{
list myList = llCSV2List(data);
myList += [llToLower(data)];
lineNum++;
llGetNotecardLine("List of commands", lineNum);
}
}

state_entry()
{
llListen(input,"","","");
llGetNotecardLine("List of Commands", lineNum);
}
listen(integer channel, string name, key id, string message)
{
string myCommand = llList2String(myList ,0);
string myResponse = llList2String(myList ,1);
if(message == myCommand) //here is where I think I need help because it's not hearing the command
{
llWhisper(0, myResponse);
}
}
}
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-13-2006 13:17
I see a couple of problems...

CODE

integer input = 1; // channel to listen to
list myList = []; // You declared myList here...
integer lineNum = 0;


default
{
dataserver(key id, string data)
{
if (data != EOF) // Not EOF, so we're still reading lines
{
list myList = llCSV2List(data); // And you declared it here as well
myList += [llToLower(data)];

...



So the second declaration 'shadows' the first one, and your global myList isn't getting updated with the stuff you're reading from the notecard. You probably want to change the 2nd line to:

CODE
myList = llCSV2List(data);


That way you're not declaring a new variable, and you'll update the global variable, which is probably what you want to do.

Then...

CODE

list myList = llCSV2List(data);
myList += [llToLower(data)];


What is this trying to do? Let's say you're reading the first line of the notecard, which is:

CODE
red,Roses are red! 

llCSV2List will make this a list of 2 elements, since there's one comma in it, so the first line will get you:

CODE
myList = ["red", "Roses are red!"];


Then in the second line you add llToLower(data) to the list again. So now the list has:

CODE
myList = ["red", "Roses are red!", "red, roses are red!"];


Note that the second entry is a single string which contains the comma. I'm pretty sure this isn't what you want. Though I'm not really sure what you want. I think just the first line, maybe?

CODE

string myCommand = llList2String(myList ,0);
string myResponse = llList2String(myList ,1);
if(message == myCommand) //here is where I think I need help because it's not hearing the command


I think it is hearing the command, but the list hasn't been set up correctly. Try putting:

CODE
llSay(0, "This is what I heard: " + message);

... as the first line in the listen event handler. That will tell you if it heard anything, and what it heard. It'll make it easier to debug.
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
03-13-2006 13:28
nm ziggy got it
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
03-13-2006 13:29
You have a few miscellaneous problems in your script, Ziggy did a good job explaining them... this script works.

Also, your dataserver event doesn't check for the proper key-- that can cause problems. It's good coding practice to never assume that you'll get the right input.

Here's what I fixed it up to:

CODE


string commandNotecard = "List of commands";
integer listenChan = 1; // channel to listen to
list commands = [];
list commandText = [];
integer lineNum = 0;
key requestId; //The dataserver request id for the current line


default
{
state_entry()
{
llListen(listenChan,"","","");
requestId = llGetNotecardLine(commandNotecard, lineNum);
}

dataserver(key id, string data)
{
if ( ( id == requestId ) && ( data != EOF ) ) //Not EOF and right data
{
list parsed = llParseString2List( data, [","], [] );
if ( llGetListLength( parsed ) != 2 ) //Uh-oh, invalid command format
{
llOwnerSay("Received mutilated command from notecard:\"" +
data + "\" line number: " + (string)lineNum );
}
else
{
commands += llList2List( parsed, 0, 0 );
commandText += llList2List( parsed, 1, 1 );
}
lineNum++;
requestId = llGetNotecardLine(commandNotecard, lineNum);
}
}

listen(integer channel, string name, key id, string message)
{
integer index = llListFindList( commands, [llToLower(message)] );
if ( index != -1 )
llOwnerSay( llList2String( commandText, index ) );
}
}