Send vector as on rez parameter

Created by Moonbuggy
== Send vector as on_rez parameter ==
this script is to demonstrate how a vector could be sent to a rezzed object
without using the listen event, sending it throught the start parameter in 
the llRezObject() function. There are two parts, the decoder that would be 
in the rezzed object to convert the start_parameter back to a vector the 
other part(s) is the encoder there are two encoders one to send a float vector
eg <1.34,4.87,9.96> and one to send a integer vector eg <13,4,999>
== Limitations ==
-- using the float encoder the maximum value that can be sent is 9.99
-- using the float encoder it is only accurate to 2 decimal places
-- using the integer encoder the maximum value that can be sent is 999
-- one big limitation at the moment is that only posative values can be sent
== Links to simmilar content ==
Key2Number Passed to rezzed objects
https://wiki.secondlife.com/wiki/Talk:Send_vector_as_on_rez_parameter
== The code ==
// [M] moonbuggy- 2012, License: GPLv3 
 
//this script is to demonstrate how a vector could be sent to a rezzed object
//without using the listen event, sending it throught the start parameter in 
//the llRezObject() function. There are two parts, the decoder that would be 
//in the rezzed object to convert the start_parameter back to a vector the 
//other part(s) is the encoder there are two encoders one to send a float vector
// eg <1.34,4.87,9.96> and one to send a integer vector eg <13,4,999>
//****LIMITATIONS****
//using the float encoder the maximum value that can be sent is 9.99
//using the float encoder it is only accurate to 2 decimal places
//using the integer encoder the maximum value that can be sent is 999
//one big limitation at the moment is that only posative values can be sent
//if anyone has any ideas to overcome these limitaions and would like to talk to me
//please IM me :) 
vector start_vec=<8,26,999>;
integer encoded;
vector decoded;
//--------------------------------------------------------------------------------------------
integer encode_float_vect(vector vec)// there is a maximum value of 9.99 in each float to send
{ 
 string v_int = "1";
 list string_split=llParseString2List((string)vec,[",","<",">"," "],[""]);
 integer i;
 for(i=0;i<3;++i)
 {
 list temp=llParseString2List(llList2String(string_split,i),["."],[""]);
 v_int+=llGetSubString(llList2String(temp,0)+llList2String(temp,1),0,2);
 }
 return (integer)v_int;
}
//--------------------------------------------------------------------------------------------
integer encode_integer_vect(vector vec)//there is a maximum value of 999 in each integer to send
{
 string v_int="-1";
 list string_split=llParseString2List((string)vec,[",","<",">"," "],[""]);
 integer i;
 for(i=0;i<3;++i)
 {
 list temp=llParseString2List(llList2String(string_split,i),["."],[""]);
 string temp_string=llGetSubString(llList2String(temp,0),0,2);
 while(llStringLength(temp_string)<3)
 {temp_string="0"+temp_string;}
 v_int+=temp_string;
 }
 return (integer)v_int;
}
//--------------------------------------------------------------------------------------------
vector decode_vector(integer int)//decode the integer into vector
{
 vector decode;
 integer n=0;
 if(int<0){n=1;}
 string vec_x=llGetSubString((string)int,1+n,3+n);
 string vec_y=llGetSubString((string)int,4+n,6+n);
 string vec_z=llGetSubString((string)int,7+n,10+n);
 if(int<0)
 {decode.x=(integer)vec_x;decode.y=(integer)vec_y;decode.z=(integer)vec_z;}
 else
 {
 decode.x=(float)(llGetSubString(vec_x,0,0)+"."+llGetSubString(vec_x,1,2));
 decode.y=(float)(llGetSubString(vec_y,0,0)+"."+llGetSubString(vec_y,1,2));
 decode.z=(float)(llGetSubString(vec_z,0,0)+"."+llGetSubString(vec_z,1,2));
 }
 return decode;
}
//--------------------------------------------------------------------------------------------
default
{
 touch_start(integer total_number)
 {
 encoded=encode_integer_vect(start_vec);
 decoded=decode_vector(encoded);
 llSay(0,"START:"+(string)start_vec);
 llSay(0,"ENCODE:"+(string)encoded);
 llSay(0,"DECODE:"+(string)decoded);
 
//IN LINE USE, BELOW IS AN EXAMPLE HOW TO ENCODE THE VECTOR AND SEND IT TO THE OBJECT DIRECTLY
 llRezObject("rezme",llGetPos()+<0,0,1>,ZERO_VECTOR,ZERO_ROTATION,encode_integer_vect(start_vec));
 
 }
 //BELOW IS AN EXAMPLE HOW TO DECODE IN THE OBJECT BEING REZZED
 // vector DECODED_VECTOR = decode_vector(llGetStartParameter());
 // another way would be to use
 //on_rez(integer start_param)
 //{
 // vector DECODED_VECTOR = decode_vector(start_param);
 //}
}