Difference between revisions of "User:Ugleh Ulrik/Vector2List"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with 'This UD Function I made creates a list of a vector. For Example list thislist = Vector2List(llGetPos()); As a list, would output list thislist = [x,y,z]; '''Function''' <pre>...')
 
m (→‎A Bad Example: Added typecasting (float to string))
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This UD Function I made creates a list of a vector.
With many requests on how to easily store a vector to a list, mainly for purposes in storing a [[llGetPos]]() only to capture the x and y cords, this is a simple way.
 
For Example  
For Example  
list thislist = Vector2List(llGetPos());
<lsl>list thislist = Vector2List(llGetPos());</lsl>
As a list, would output
As a list, would output
list thislist = [x,y,z];
<lsl>list thislist = [x, y, z];</lsl>


'''Function'''
'''Function'''
<pre>
<lsl>list Vector2List(vector v){
    return [v.x, v.y, v.z];
}</lsl>
 
===A Bad Example===
'''Do Not Do This'''
<lsl>
list Vector2List(vector v){  
list Vector2List(vector v){  
     list alist = llParseString2List((string)v,[",", ">", "<"],[]);
     return [v.x, v.y, v.z];
return alist;
}
 
default
{
 
    touch_start(integer total_number)
    {
        vector pos = llGetPos();
        list thislist = Vector2List(pos);
 
        string x = llList2String(thislist,0);
        string y = llList2String(thislist,1);
        string z = llList2String(thislist,2);
        llOwnerSay("X Axis: " + x);
        llOwnerSay("Y Axis: " + y);
        llOwnerSay("Z Axis: " + z);
    }
}
}
</pre>
</lsl>
 
'''Here is the correct way to do this'''
 
<lsl>
default
{


'''An Example'''
    touch_start(integer total_number)
<pre>
    {
        vector pos = llGetPos();
        llOwnerSay("X Axis: " + ((string)pos.x));
        llOwnerSay("Y Axis: " + ((string)pos.y));
        llOwnerSay("Z Axis: " + ((string)pos.z));
    }
}
</lsl>
 
===A Good Example===
<lsl>
list Vector2List(vector v){  
list Vector2List(vector v){  
     list alist = llParseString2List((string)v,[",", ">", "<"],[]);
     return [v.x, v.y, v.z];
return alist;
}
}
default
default
{
{
Line 24: Line 63:
     touch_start(integer total_number)
     touch_start(integer total_number)
     {
     {
      list thislist = Vector2List(llGetPos());
        list size = Vector2List(llGetScale());
 
        list sorted_size = llListSort(size, 1, 0);
    string x = llList2String(thislist,0);
        llOwnerSay("<"+llList2CSV(sorted_size)+">");
    string y = llList2String(thislist,1);
    string z = llList2String(thislist,2);
llOwnerSay("X Axis: " + x);
llOwnerSay("Y Axis: " + y);
llOwnerSay("Z Axis: " + z);
     }
     }
}
}
</pre>
</lsl>

Latest revision as of 01:35, 22 April 2010

With many requests on how to easily store a vector to a list, mainly for purposes in storing a llGetPos() only to capture the x and y cords, this is a simple way.

For Example <lsl>list thislist = Vector2List(llGetPos());</lsl> As a list, would output <lsl>list thislist = [x, y, z];</lsl>

Function <lsl>list Vector2List(vector v){

   return [v.x, v.y, v.z];

}</lsl>

A Bad Example

Do Not Do This <lsl> list Vector2List(vector v){

   return [v.x, v.y, v.z];

}

default {

   touch_start(integer total_number)
   {
       vector pos = llGetPos();
       list thislist =  Vector2List(pos);
       string x = llList2String(thislist,0);
       string y = llList2String(thislist,1);
       string z = llList2String(thislist,2);
       llOwnerSay("X Axis: " + x);
       llOwnerSay("Y Axis: " + y);
       llOwnerSay("Z Axis: " + z);
   }

} </lsl>

Here is the correct way to do this

<lsl> default {

   touch_start(integer total_number)
   {
       vector pos = llGetPos();
       llOwnerSay("X Axis: " + ((string)pos.x));
       llOwnerSay("Y Axis: " + ((string)pos.y));
       llOwnerSay("Z Axis: " + ((string)pos.z));
   }

} </lsl>

A Good Example

<lsl> list Vector2List(vector v){

   return [v.x, v.y, v.z];

}

default {

   touch_start(integer total_number)
   {
       list size = Vector2List(llGetScale());
       list sorted_size = llListSort(size, 1, 0);
       llOwnerSay("<"+llList2CSV(sorted_size)+">");
   }

} </lsl>