Jump to content
  • 0

i have a scripting question


Tiggy3
 Share

You are about to reply to a thread that has been inactive for 2132 days.

Please take a moment to consider if this thread is worth bumping.

Question

6 answers to this question

Recommended Posts

  • 0

Freya's right.  llSubStringIndex is your friend.  However, it's wise to avoid doing this sort of keyword search in any script that is listening on the public chat channel (channel 0).  A script that has to listen to everything within chat range and parse each message in that way slows down the server and contributes to chat lag for everyone.  If you are parsing messages for a key word, it's usually best to either on ly listen on a unique channel (like 993827, maybe) or to listen for a very brief time (like during the 10 seconds after a user clicks a button to trigger your listener).

Link to comment
Share on other sites

  • 0

The previous answers are incorrect  and it was bothering me so here is the correct answer.  Reading llSubStringInde in the wiki will only frustrate you. 

Problem 1. in your listener you see the third param is type of string named message.  This is what is said in chat

listen(integer channel, string name, key id, string message) {

in your call to llSubStringIndex("string data","XXXXX"); you are passing in a string "string data".  since "string data" is never in "XXXXX"  this will always retrurn -1 (not found)  Pass in the variable created in the third param of the listener which is named message like this (i changed XXXXX to test to make testing easier for you)

    integer index = llSubStringIndex(message,"test")  

Now say test in local chat and you will "test was not found in the string."  In local chat.  This is not what you want of couse.  You really want the opposite so now we have a new problem

Problem 2  Your condition is equal to -1 or string not found

    if(index == -1) {

What you want to say here is if index is not equal to -1 then rez the object.  So change your condition from is equal to (==) to is not equal to (!=) and  BOOM! it works

if(index != -1) {

Here is the whole code enjoy

default
{

state_entry() {
    llListen(0,"", NULL_KEY, "");
}

listen(integer channel, string name, key id, string message) {
    integer index = llSubStringIndex(message,"test");
    if(index != -1) {
        // This line will pick the first object out of the container and rez it
        llRezObject(llGetInventoryName(INVENTORY_OBJECT,0), llGetPos()+<0,0,.2>,ZERO_VECTOR,ZERO_ROTATION,0);
    }
    else {
        llSay(PUBLIC_CHANNEL,"test was not found in the string.");
    }
}

}

 

  • Like 1
Link to comment
Share on other sites

  • 0

Disregard my previous answer, while I did properly address the issue of why it always rezzes the object, I briefly saw a response to my post chastising me as the original post did not include the snippet I corrected and that I should not advocate the use of channel 0.

First I apologize to those I offended by using the work incorrect.  Now I attempt to answer the first question. One of the problems with anything produced by Linden Labs is the "good enough" approach to everything.  Rarely using this forum I would like to see edits stand out from original post but I digress.  I am hoping this is the true original question:

******Orignial Post******
i am using the " if " operator to search chat text for a key word and cause an event to happen. The problem is, it will only work if that is the first and only word. I want it to find the key word anywhere in chat. I am using the followin statement where xxx is my key word:

listen(integer channel, string name, key id, string message) {
if (message == "XXXXXXX") {

im new to this and cant get the syntax right. im using this:
***********End Orignal Post ******

First I would have asked for the whole code you are working with:  I would suggest that you change "XXXXXXX" to something more manageable since the == has to match exactly and XXXXXX XXXXXXX are not the same and it is  easy to make mistakes.   Common test patterns are foo with bar being the result consider the following functional code:

listen(integer channel, string name, key id, string message) {
   if(message == "foo"){
       llOwnerSay("bar");
    }else{
        llOwnerSay("Jim I am a surgeon not a magician");          
    }    
}

This snip is using the same syntax as the original post (and example from the wiki) and as long as we are using best practices here, far more technically correct than the suggestion of llSubStringIndex. Contrary to the answer llSubStingIndex is not your friend and has zero value in this program.  It is simply am massive waste of size and resource.  In any form of computer science; when performing a simple conditional check such as this one; an if/then/else is far more resource efficient than a complex function performing a needle in haystack compare.  

So llSubString is not a good solution to this problem.  Since the original snippet was valid syntax we need to review the complete original code to find real error.  This is the true correct answer and I apologize for jumping on a truly bad solution.

Finally on the big old NO-NO! of using Channel 0.  Rolig is correct is not a best practice, however, there are times when using Channel 0 is valid.  This question is a perfect example, when we are in development mode we can make things much easier on ourselves when applying the KISS principal or Keep It Simple Stupid.  So while working out simple issues nothing wrong with saying something in local chat.  Best to move it to private channels latter... unless your project actually needs to listen to channel 0.  

I would love to discuss my views with anyone in world.  Hit me up

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2132 days.

Please take a moment to consider if this thread is worth bumping.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...