Difference between revisions of "Check if input is an vector"

From Fire And Ice Grid
Jump to navigation Jump to search
 
 
Line 14: Line 14:
  
 
=== Using The Script ===
 
=== Using The Script ===
The functionality for this script is contained in the method (sometimes known as a user-defined function or UDF) at the top. Everything inside state_entry is there purely to demonstrate how the function might be used. If adding this to a preprocessor along with the [[Check if the input is a float|check float script]] be aware that already forms part of this script and should be used in that way. No need to duplicate it.  
+
The functionality for this script is contained in the method (sometimes known as a user-defined function or UDF) at the top. Everything inside state_entry is there purely to demonstrate how the function might be used. If adding this to a pre-processor along with the [[Check if the input is a float|check float script]] be aware that already forms part of this script and should be used in that way. No need to duplicate it.  
 +
 
 +
=== Script On Github ===
 +
[https://github.com/Fire-And-Ice-Grid/LSL-And-OSSL-Script-Library LSL And OSSL Script Library On GitHub]
  
 
==== Licence ====
 
==== Licence ====

Latest revision as of 23:16, 16 January 2021


This is a short LSL script snippet which will work in both Opensimulator and Second life. It takes user input and then verifies whether it can be correctly used as vector.
If you prefer to download the file it is available along with others on my GitHub Page. This is a useful script to have in your LSL preprocessor folder.

Using The Script

The functionality for this script is contained in the method (sometimes known as a user-defined function or UDF) at the top. Everything inside state_entry is there purely to demonstrate how the function might be used. If adding this to a pre-processor along with the check float script be aware that already forms part of this script and should be used in that way. No need to duplicate it.

Script On Github

LSL And OSSL Script Library On GitHub

Licence

 1 BSD 3-Clause License
 2 Copyright (c) 2020, Sara Payne
 3 All rights reserved.
 4 Redistribution and use in source and binary forms, with or without
 5 modification, are permitted provided that the following conditions are met:
 6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 7 2. Redistributions in binary form must reproduce the above copyright notice,
 8    this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 9 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
11 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
13 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
14 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
18 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Script

 1 /*
 2 Check If A User Input is a Vector
 3 ===================================
 4 Any time you need to take user input it is paramount you check they have actually put something sensible. 
 5 All user input in Opensim comes in the form of a string, either from a notecard, listening to chat or a text box. 
 6 This function takes that user input and validates it as being valid to typecast (convert) to a vector.
 7 Caveat - Only works in base 10 (decimal);
 8 */
 9 
10 integer CheckIsFloat (string inputString)
11 {   //checks to see if the provided string can be type cast (converted) into a float
12     list allowedChars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."]; //no other characters allowed in a decimal float other than "-" which is dealt with another way
13     integer charOk = TRUE;
14     if (llGetSubString (inputString, 0, 0) == "-") 
15     {   //if this is a negative number thats ok, but we don't need it for the check, remove it. 
16         inputString = llGetSubString(inputString, 1, -1); //removes the - from the start
17     }//close if number is negative
18     integer decimalIndex = llSubStringIndex(inputString, "."); //get the index of the decimal if one exists
19     if (decimalIndex != -1)
20     {   //only come here if the number has a decimal in it
21         string afterFirstDecimal = llGetSubString (inputString, decimalIndex+1, -1);
22         if (llSubStringIndex(afterFirstDecimal, ".") !=-1)  
23         {   //come here if there is more than one . in the string... aka not a valid decimal
24             charOk = FALSE; //set return value to false
25         }//close if there is more than one "." 
26     }//close if the string has a "."
27     integer charIndex = 0;
28     while (charOk && charIndex < llStringLength(inputString))
29     {   //loops through the characters in the string, checking each one is part of the allowed characters
30         //if any of them don't match charOK is set to false. 
31         string charToCheck = llGetSubString(inputString, charIndex, charIndex); //sets the current character to check
32         if(!(~llListFindList(allowedChars, (list)charToCheck))) //checks selected character against the allowed characters
33         {   //come here if the character is not in the allowed characters list. 
34             charOk = FALSE; //set result to FALSE
35         }//close if character is not in the allowed list
36         ++charIndex; //increase the character count   
37     }//close while loop
38     return charOk; // return TRUE/FALSE
39 }//close CheckIsFloat
40 
41 integer CheckIsVector (string inputString)
42 {   //checks to see if a string provided can be cast to an Euler vector, returning TRUE if it can and FALSE if it can't.
43     //This function works with a CheckIsFloat method availible seperately
44     integer isVector;
45     string startProcess = llStringTrim ((inputString), STRING_TRIM); //remove any white space from either side of the string
46     integer stringLength = llStringLength(startProcess); //find the number of characters in the string
47     integer openBraceIndex = llSubStringIndex(startProcess, "<");//find the index number of the <
48     integer closeBraceIndex = llSubStringIndex(startProcess, ">");//find the index number of the >
49     if (openBraceIndex == 0 && closeBraceIndex == stringLength-1 )
50     {   //only come here if the first character is < and the last character is >
51         integer startIndex = openBraceIndex +1; //character after the <
52         integer endIndex = closeBraceIndex -1; //character before the >
53         startProcess = llGetSubString(startProcess, startIndex, endIndex); //removes the "<>" from the startProcess string
54         list stringParts = llCSV2List(startProcess); //converts the remaining numbers which are CSV into list elements
55         string strX =  llStringTrim( (llList2String(stringParts,0)), STRING_TRIM); //defines a string for X and removes any white space
56         string strY =  llStringTrim( (llList2String(stringParts,1)), STRING_TRIM); //defines a string for y and removes any white space
57         string strZ =  llStringTrim( (llList2String(stringParts,2)), STRING_TRIM); //defines a string for z and removes any white space
58         string strS =  llStringTrim( (llList2String(stringParts,3)), STRING_TRIM); //defines a string for s and removes any white space
59         integer strXIsFloat =  CheckIsFloat(strX); //returns a true or false, if its true this value is a float. 
60         integer strYIsFloat =  CheckIsFloat(strY); //returns a true or false, if its true this value is a float.
61         integer strZIsFloat =  CheckIsFloat(strZ); //returns a true or false, if its true this value is a float.
62         if (strXIsFloat && strYIsFloat && strZIsFloat) isVector  = TRUE; //if all values are floats then this can be type cast into a rotation, return true
63         else isVector  = FALSE; //not all values inside the <> are floats, so this can not be type cast to a rotation, set false
64     }//close if containing characters are <>
65     else isVector = FALSE; //first and last characters did not match vector formatting, so it can't be a valid rotation, set to false
66     return isVector; //returns true/false
67 }
68 
69 default
70 {
71     state_entry()
72     {
73         string userInputString = "<1,1,1>"; //sample input from a user
74         integer isVector = CheckIsVector(userInputString); //calling the checking method
75         integer userInputVector; // defines the new rotation for this example
76         if (isVector) 
77         {   //if the method returned TRUE come here
78             userInputVector = (integer)userInputString; //typecast the string to a rotation
79             llOwnerSay("This is a vector");
80         }//close if user input was a rotation
81         else 
82         { 
83             //send error to user and get them to try again . 
84             llOwnerSay("This is not a vector");
85         }
86     }
87 }

Syntax Highlighting

GeShi syntax highlighting is enabled on this wiki. <be>

When using the tabs described in the link, change the language part to "lsl". Eg.
syntaxhighlight lang="lsl" line
inside angled brackets <>