Difference between revisions of "LlDetermineType"

From Second Life Wiki
Jump to navigation Jump to search
m
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<pre>
{{LSL_Function
integer llDetermineType(string source, integer start, integer end);
|inject-2={{LSL_Function/negative_index|false|start|end|self=*}}
if(llDetermineType(msg,0,-1)==1)
|func_id|func_sleep=0.0|func_energy=0.0|func=llDetermineType
{
    integer new_number = msg;
    //llDetermineType determines the type of a part of a string and returns the result. In this istance, returning 1 will indicate that it is an integer instead of a key or rotation.
}
</pre>
{{LSL_Function/negative_index|false|number|self=*}}{{LSL_Function
|func_id
|mode=request
|mode=request
|func_sleep=0.0
|func_energy=10.0
|func=llDetermineType
|sort=
|sort=
|p1_type=string
|p1_type=string|p1_name=source|p1_desc= The string that you are determining the type of a part of.
|p1_name=source
|p2_type=integer|p2_name=start|p2_desc= The start point in the string of what you are determining.
|p2_type=integer
|p3_type=integer|p3_name=end|p3_desc= The end point in the string of what you are determining.
|p2_name=start
|p3_type=integer
|p3_name=end
|p1_desc= The string that you are determining the type of a part of.
|p2_desc= The start point in the string of what you are determining.
|p3_desc= The end point in the string of what you are determining.
|func_footnote=
|func_footnote=
|func_desc
|func_desc
|return_text=to represent the type of the part of the string.
|return_text=to represent the type of the part of the string.
|return_type=integer
|return_type=integer
|constants={{LSL_Constants/ListEntryType}}
|spec
|spec
|caveats
|caveats=
|examples=<pre>
|examples=
<lsl>
default
default
{
{
Line 36: Line 22:
         llListen(0,"",llGetOwner(),"");
         llListen(0,"",llGetOwner(),"");
     }
     }
     listen(integer chan, string name, key id, string msg){if(llDetermineType(msg,0,-1)==1){integer new_number=msg;}}}</pre>
     listen(integer chan, string name, key id, string msg)
|helpers
    {
        if(llDetermineType(msg,0,-1) == TYPE_INTEGER)
        {
            integer new_number = (integer)msg;
            vector also = <new_number,new_number*8,new_number/3>;
            llOwnerSay("The number you just said was "+(string)new_number+" and put in a vector, it is "+also+".");
        }
    }  
}
</lsl>
|helpers=
<lsl>
// Released into the public domain by Adam Wozniak and Doran Zemlja in 2010
// Free for anyone to use for any purpose they like.
//
// LSL implementation of llDetermineType
 
integer dzDetermineType(string s) {
  integer n;
 
  // is it an integer?
 
  if ((string)((integer) s) == s) {
      return TYPE_INTEGER;
  }
 
  // is it a rotation or a vector?
  list parsed = llParseString2List(s, [","], []);
 
  if (llSubStringIndex(s, "<") == 0) {
      // is it a vector?
      if (llGetListLength(parsed) == 3) {
        return TYPE_VECTOR;
      }
      // is it a rotation?
      if (llGetListLength(parsed) == 4) {
        return TYPE_ROTATION;
      }
  }
 
  // is it a key?
  parsed = llParseString2List(s, ["-"], []);
 
  if (llStringLength(s) == 36 && llGetListLength(parsed) == 5) {
      return TYPE_KEY;
  }
 
  // is it a float?
  n = 0;
  if (llSubStringIndex(s, "-") == 0) {
      n = 1;
  }
  parsed = llParseStringKeepNulls(llGetSubString(s, n, -1), ["."], []);
 
  if (llGetListLength(parsed) == 2) {
      string i = "1" + llList2String(parsed, 0);
      string f = "1" + llList2String(parsed, 1);
      if (((string)((integer) i) == i) && ((string)((integer) f) == f)) {
        return TYPE_FLOAT;
      }
  }
 
  // must be a string then
  return TYPE_STRING;
}
</lsl>
|related
|related


|notes=Should make it easier for scripters to determine the types of parts of strings in order to get accurate results as opposed to manually deriving them from the string.
|notes=Should make it easier for scripters to determine the types of parts of strings in order to get accurate results as opposed to manually deriving them from the string. Could anyone delete the unnecessary parts to this page? This is my first time editing and I can't find it :P


|comments=The trouble is, that when it comes to floats it's hard to tell them from integers if the decimal has been stripped. Or strings from keys... or strings that look like any of the other types. You can guess but you can't be sure; if you are building a function on top of logic that isn't perfect then you end up with something that isn't all that useful. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 11:50, 5 May 2009 (UTC)
|cat1
|cat1
|cat2
|cat2
}}
}}

Latest revision as of 19:08, 10 June 2012

Emblem-important-yellow.png LSL Feature Request
The described function does not exist. This article is a feature request.

Summary

Function: integer llDetermineType( string source, integer start, integer end );

Returns an integer to represent the type of the part of the string.

• string source The string that you are determining the type of a part of.
• integer start The start point in the string of what you are determining.
• integer end The end point in the string of what you are determining.

start & end do not support negative indexes.

Type Description
TYPE_INTEGER 1 integer
TYPE_FLOAT 2 float
TYPE_STRING 3 string
TYPE_KEY 4 key
TYPE_VECTOR 5 vector
TYPE_ROTATION 6 rotation
TYPE_INVALID 0 none

Caveats

  • If either start or end are out of bounds the script continues to execute without an error message.

Examples

<lsl> default {

   state_entry()
   {
       llListen(0,"",llGetOwner(),"");
   }
   listen(integer chan, string name, key id, string msg)
   {
       if(llDetermineType(msg,0,-1) == TYPE_INTEGER)
       {
           integer new_number = (integer)msg;
           vector also = <new_number,new_number*8,new_number/3>;
           llOwnerSay("The number you just said was "+(string)new_number+" and put in a vector, it is "+also+".");
       }
   }    

}

</lsl>

Useful Snippets

<lsl> // Released into the public domain by Adam Wozniak and Doran Zemlja in 2010 // Free for anyone to use for any purpose they like. // // LSL implementation of llDetermineType

integer dzDetermineType(string s) {

  integer n;
  // is it an integer?
  if ((string)((integer) s) == s) {
     return TYPE_INTEGER;
  }
  // is it a rotation or a vector?
  list parsed = llParseString2List(s, [","], []);
  if (llSubStringIndex(s, "<") == 0) {
     // is it a vector?
     if (llGetListLength(parsed) == 3) {
        return TYPE_VECTOR;
     }
     // is it a rotation?
     if (llGetListLength(parsed) == 4) {
        return TYPE_ROTATION;
     }
  }
  // is it a key?
  parsed = llParseString2List(s, ["-"], []);
  if (llStringLength(s) == 36 && llGetListLength(parsed) == 5) {
     return TYPE_KEY;
  }
  // is it a float?
  n = 0;
  if (llSubStringIndex(s, "-") == 0) {
     n = 1;
  }
  parsed = llParseStringKeepNulls(llGetSubString(s, n, -1), ["."], []);
  if (llGetListLength(parsed) == 2) {
     string i = "1" + llList2String(parsed, 0);
     string f = "1" + llList2String(parsed, 1);
     if (((string)((integer) i) == i) && ((string)((integer) f) == f)) {
        return TYPE_FLOAT;
     }
  }
  // must be a string then
  return TYPE_STRING;

} </lsl>

Notes

Should make it easier for scripters to determine the types of parts of strings in order to get accurate results as opposed to manually deriving them from the string. Could anyone delete the unnecessary parts to this page? This is my first time editing and I can't find it :P

Deep Notes

Signature

//function integer llDetermineType( string source, integer start, integer end );

Comments

The trouble is, that when it comes to floats it's hard to tell them from integers if the decimal has been stripped. Or strings from keys... or strings that look like any of the other types. You can guess but you can't be sure; if you are building a function on top of logic that isn't perfect then you end up with something that isn't all that useful. -- Strife (talk