Difference between revisions of "Typecast"

From Second Life Wiki
Jump to navigation Jump to search
(Add Examples)
Line 101: Line 101:
== Examples ==
== Examples ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<lsl>string a = "1.5";
<lsl>
string a = "1.5";
float b = (float)a;
float b = (float)a;
integer c = (integer)a;</lsl>
integer c = (integer)a;
 
integer i;
i = (integer) 1.23;    // 1
i = (integer) -1.23;    // -1
i = (integer) "0123";  // 123
i = (integer) "0x12A";  // 298
 
float f;
f = (float) "6.2e1";  // 62.0
// "6.2e1", "6.2e+1", "6.2E1", "6.2E+1" are all equivalent.
 
string s;
s = (string) [1, 2.3, "a"];    // "12.300000a"
s = (string) <1.0, 2.3, 4.56>; // "<1.00000, 2.30000, 4.56000>"
 
list l;
l = (list) "";                // [""]
l = (list) <1.0, 2.3, 4.56>;  // ["<1.00000, 2.30000, 4.56000>"]
 
vector v;
v = (vector) "<1.0, 2.3, 4.56>";  // <1.0, 2.3, 4.56>
v = (vector) "<1.0, 2.3>";        // ZERO_VECTOR  (Due to insufficient value)
 
rotation r;
r = (rotation) "<1.0, 2.3, 4.56, 1.0>";  // <1.0, 2.3, 4.56, 1.0>
r = (rotation) "<1.0, 2.3, 4.56>";      // ZERO_ROTATION  (Due to insufficient value)
</lsl>
</div></div>
</div></div>



Revision as of 06:10, 17 April 2009

To convert the type of a value a typecast is required. There are two types of typecasting, explicit and implicit. Explicit typecasts must be provided by the programmer, but implicit typecasts are put in place by the compiler. LSL implicitly typecasts strings to keys and integers to floats where the latter type is required but the former is provided.

Supported Typecasts
To
integer float string key list vector rotation
From integer x x x x
float x x x x
string x x x x x x x
key x x x
list x x
vector x x x
rotation x x x


Syntax: (type)value

Converts value to type.

• expression type variable type
• expression value expression or constant

If value is a complex expression, it may be beneficial to wrap it in parentheses. (type)(value)

Examples

<lsl> string a = "1.5"; float b = (float)a; integer c = (integer)a;

integer i; i = (integer) 1.23; // 1 i = (integer) -1.23; // -1 i = (integer) "0123"; // 123 i = (integer) "0x12A"; // 298

float f; f = (float) "6.2e1"; // 62.0 // "6.2e1", "6.2e+1", "6.2E1", "6.2E+1" are all equivalent.

string s; s = (string) [1, 2.3, "a"]; // "12.300000a" s = (string) <1.0, 2.3, 4.56>; // "<1.00000, 2.30000, 4.56000>"

list l; l = (list) ""; // [""] l = (list) <1.0, 2.3, 4.56>; // ["<1.00000, 2.30000, 4.56000>"]

vector v; v = (vector) "<1.0, 2.3, 4.56>"; // <1.0, 2.3, 4.56> v = (vector) "<1.0, 2.3>"; // ZERO_VECTOR (Due to insufficient value)

rotation r; r = (rotation) "<1.0, 2.3, 4.56, 1.0>"; // <1.0, 2.3, 4.56, 1.0> r = (rotation) "<1.0, 2.3, 4.56>"; // ZERO_ROTATION (Due to insufficient value) </lsl>

Example n°2

<lsl> integer BOOT_TIME; float BOOT_TIME_2; string BOOT_T ; string BOOT_CHAN;

default

{

   state_entry()
   {
      BOOT_TIME = llGetUnixTime(); 
      BOOT_T = (string)BOOT_TIME;
      BOOT_TIME_2 = llGetTimeOfDay();
      BOOT_CHAN = (string)BOOT_TIME_2;
        llSetObjectDesc( BOOT_T);
        llSetText(BOOT_CHAN,<1.0,0.0,0.0>,1.0);
   }

   

} </lsl>

Caveats

  • The compiler allows explicit typecasting where it is not needed and does not optimize it out. Unnecessary typecasts will bloat code and slow it down.

Notes

  • For getting at the elements of a list use the llList2* functions.