RLib 2.0 - A LSL Developer Library
Created by Hitani Fugazi
Created by Hitani Fugazi
RLib is designed to make your lives easier, by allowing you to write in LSL logically, without having to stress over unimportant, yet essential details. Simply copy the RLib source to the top of any script you wish to use it in, and you will be able to use all of these functions with ease. RLib has been written carefully, taking every possible precaution to make sure that it is functional, flexible, and will not clutter your code.
Questions, comments, and concerns to:
[email]instinkt.aka.ink@gmail.com[/email]
Function List
string rl_GetFirstName ( key id )
Retrieves the first name of an avatar
Parameters:
- id - key of the avatar
Returns:
- The first name of the avatar specified, as a string
string rl_GetLastName ( key id )
Retrievs the last name of an avatar
Parameters:
- id - key of the avatar
Returns:
- The last name of the avatar specified, as a string
string rl_Replace ( string str, string pattern, string alternative )
Replaces a pattern in a string with an alternative pattern
Parameters:
- str - The string
- pattern - The pattern to find
- alternative - The string to replace pattern with
Returns:
- The string with pattern replaced by alternative
Example:
- rl_Replace( "the bar", "the", "foo" ) = "foo bar"
- rl_Replace( "Welcome, come right in!", "come", "fly" ) = "Welfly, fly right in!"
integer rl_RandomInteger ( integer min, integer max )
Retrieves a random integer between min and max, inclusively
Parameters:
- min - The minimum value of the integer
- max - The maximum value of the integer
Returns:
- A random integer from min to max
list rl_Int2Bin ( integer num )
Converts a signed 32-bit integer into a list of integers representing each bit
Parameters:
- num - An integer to convert
Returns:
- A list of 'bits' ( integers with a value of 1 or 0 )
Example:
- rl_Int2Bin( 32 ) = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
- rl_Int2Bin( 21474836487 ) = [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Notes:
- Signed integers are not supported by LSL. Becuase of this, num has a maximum value of 21474836487
- Although the maximum value uses only 31 bits, to prevent incompatibility with future updates, the list returned by this function will always have a length of 32
- An integer with a value of 0 is FALSE
- An integer with a value of 1 is TRUE
- 'if ( a == TRUE )' is the same as 'if ( a )'
integer rl_Bin2Int ( list bin )
Converts a list of bits into a signed integer
Parameters:
- bin - A list of bits, either TRUE ( 1 ) or FALSE ( 0 )
Returns:
- An integer represented by the bits provided
Example:
- rl_Bin2Int( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ) = 32
- rl_Bin2Int( [1, 0, 0, 0, 0, 0] ) = 32
Notes:
- The list bin does not have to have a length of 32, any extra FALSE values proceeding the first occurrence of 1 in the list may be omitted
- The list bin cannot have a length higher than 31
- Regardless of the length of the length of bin, 'llList2Key( bin, llGetListLength( bin ) - 1 )' should represent 'llPow( 2, 0 )', or 2 to the 0 power
string rl_Trim ( string str )
Removes leading and trailing spaces from a string
Parameters:
- str - The string to trim
Returns:
- A string with no leading or trailing spaces
Source:
Place this at the top of any script to use it there
CODE
string rl_GetFirstName ( key id )
{
return llGetSubString( llKey2Name( id ), 0, llSubStringIndex( id, " " ) - 1 );
}
string rl_GetLastName ( key id )
{
return llGetSubString( llKey2Name( id ), llSubStringIndex( id, " " ) + 1, - 1 );
}
// Submitted by Christopher Omega
string rl_Replace ( string str, string pattern, string alternative )
{
return llDumpList2String( llParseStringKeepNulls( str, [pattern], [] ), alternative );
}
integer rl_RandomInteger ( integer min, integer max )
{
return llFloor( llFrand( max - min + 1 ) ) + min;
}
// Submitted by Strife Onizuka
list rl_Int2Bin ( integer num )
{
integer i = 32;
list binary = [];
do
{
binary += !!( num & ( 1 << --i ) );
} while ( i );
return binary;
}
// Submitted by Strife Onizuka
integer rl_Bin2Int ( list bin )
{
integer val = 0;
integer i = llGetListLength( bin );
while( i )
{
if(llList2Integer( bin, -( i-- ) ) ) //allows for the use of hex strings
{
val = val | ( 1 << i );
}
}
return val;
}
// Submitted by Sol Columbia
string rl_Trim ( string str )
{
if ( llGetSubString( str, 0, 0 ) == " " )
{
str = rl_Trim( llDeleteSubString( str, 0, 0 ) );
}
if ( llGetSubString( str, -1, -1 ) == " " )
{
str = rl_Trim( llDeleteSubString( str, -2, -1 ) );
}
return str;
}
Credits:
- Hitani Fugazi
- Christopher Omega
- Strife Onizuka
- Sol Columbia