Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

An Idea I have for a Script

Cruven Garden
Registered User
Join date: 7 Jun 2005
Posts: 6
07-29-2005 02:36
I don't know how to script so what I can't do myself I find someone smarter to do it for me. I need a script that is well commented so it can be modified by the average user. I will make the script a freebie so if you want credit as the creator, put that in the comments. What I need is a script that when put in any object, if you say c1 it will say a different command like typing in the chat field and press enter. also need the same for c2, c3, etc. I need it for c0 - c9. Can this be done?
Minsk Oud
Registered User
Join date: 12 Jul 2005
Posts: 85
07-29-2005 03:15
So something along the lines of this example, but with multiple if statements? Simply replacing the listen body with something like:

CODE

if (message == "c1") {
llSay(0, "Message 1");
}
else if (message == "c2") {
llSay(0, "Message 2");
}
// etc.
should give you what you want. You may need to change the llListen to accept messages from avatar's other than its owner.
Catherine Omega
Geometry Ninja
Join date: 10 Jan 2003
Posts: 2,053
07-29-2005 03:49
Here you go. I think this is pretty well commented, but you may want to look up some of the terms on the LSL wiki.

CODE
// This script listens for chat from either the owner or any speaker, depending on settings.
// When chat is recieved, the script compares the message to a list of predefined keywords.
// If a keyword is a match, the script responds with the corresponding response from a second list, containing responses.

// Global variables exist outside of a state.
// They are named in the format "gVariableName" to distinguish them as global variables.

// ------ Edit below this line --------

// lists are enclosed in [square brackets] and strings (in this case, chat text) are enclosed in quotation marks: ""
list gKeywords = ["hello", "goodbye", "1"]; // this is a list of keywords which will trigger the response.
// For best results, write keywords in lower case. That way, when the user says them,
// they can be automatically converted to lower case and compared with this list easily.
// Keywords can be of any length and you can have as many keywords as you want.
// You only need to make sure there's a corresponding entry in the response list.
list gResponses = ["Hi there.","See you later.", "One."]; // corresponding responses, in the same order as the keyword list.

integer gInputChannel = 0; // chat channel to listen on.
integer gOutputChannel = 0; // chat channel to output chat on.

integer gUseOwner = TRUE; // only listen to the object owner? (Set FALSE to listen to chat from any user or object.)

// ------ Edit above this line --------

// this is the default state. It's the first thing to run when the script starts.
default
{
// this event handler runs when entering this state.
state_entry()
{
// check to see if the keyword and response lists are the same length.
// this makes sure the most obvious problems won'
if (llGetListLength(gKeywords) == llGetListLength(gResponses)
{
// check to see if we're supposed to be listening to the object owner.
if (gUseOwner == TRUE)
{
// listen on the input channel for any message from the object owner.
llListen(gInputChannel,"",llGetOwner(),"");
}

// if not, we can listen to chat from any user or object.
else
{
// listen on the input channel for any message from anyone and anything.
llListen(gInputChannel,"","","");
}
}

// if the keyword and response lists aren't the same length, we go here.
// note that there is no llListen function here -- the script is NOT listening.
else
{
// tell the object owner only that they've messed up the keyword and response lists.
llOwnerSay("Error: The lists of keywords and responses are not the same length. You need an equal number of keywords and responses.");
}
}

// if llListen is set, this event handler is triggered when chat is recieved.
listen(integer channel, string name, key id, string message)
{
message = llToLower(message); // Convert message to lower case.
// This allows us to respond to "hello", "HELLO" or "hEllO" as if they were all written the same way.

// Get the index of the first instance of the message within the keyword list.
// If nothing is found, the index will be -1.
// We COULD put this llListFindList call in the IF statement immediately below,
// but the reason we're doing this as a seperate variable is so that we only have to call llListFindList once.
integer index = llListFindList(gKeywords,[message]);

// now we need to check the result -- is it more than -1?
if (index > -1)
{
// If we got here, it means that the message IS on the keywords list.

// Get the response located at the corresponding position to the keyword index.
// Assuming the keyword and response lists have been set up correctly, this will be the intended response to the keyword.
string response = llList2String(gResponses,index);
// See, storing the result of llListFindList above as a variable means that the data exists here, and we don't
// need to use llListFindList to find out data we already have.
// This is good, because it means the script needs to do less work.

// say the response on the output channel
llSay(gOutputChannel,response);
}
}
}
_____________________
Need scripting help? Visit the LSL Wiki!
Omega Point - Catherine Omega's Blog
Branduff Kojima
probably doesn't get it.
Join date: 1 Apr 2004
Posts: 89
07-29-2005 04:17
From: Catherine Omega
Here you go. I think this is pretty well commented, but you may want to look up some of the terms on the LSL wiki.

that is pretty well commented.
Cruven Garden
Registered User
Join date: 7 Jun 2005
Posts: 6
Thanks Everyone
07-29-2005 05:35
Thank You, but I fould another way instead of using scripts to do what I described. I just create a gesture for it.