Watch more videos of Second Life
Monday, February 2, 2009
Wednesday, January 14, 2009
How to keep track of primitives within an object
Why use this script?
If you are ever linking many different primitives together and plan on having these primitives communicate with one another via linked messages, you may find it useful to have a logical method for tracking the object numbers. Perhaps using the objects name as its identifier is easier for the developer who may be adding objects to a primitive as they go. For the code, jump to the bottom.
How Linking Works
When an object is linked, each primitive within the object is assigned an integer as its unique identifier for the llMessageLinked(integer object_number, integer any_number, string your_message, key _avatarid). That first parameter of this function can be LINK_ALL, LINK_THIS, or a valid integer identifying a primitive in the object. How do you know the integer number? Second Life automatically assigns this integer number by making the last object clicked the integer "1" as its identifier, the second to last object clicked the "2", and you get the idea. The last primitive clicked(the root primitive) is the primitive whose position and size is shown when you right click and click edit from the selection wheel.
How to use this script
Below is the code you can put into a script above the default option. This will give you the option to use the private function
link("The Name of the Primitive You Want");
After this function is called. You will then have the global variable called int_missing_link, which can be used in a llMessageLinked command. This function allows me to never worry about a linknumber changing and having to click the objects in the right order to get the link numbers back to the appropriate number.
For the example below let us assume we have this small object with three primitives
The link command for the
Within the Root Prim put the following script
integer north_wall;
integer south_wall;
integer east_wall;
integer int_missing_link; // this is a global variable we want to know based on primitive name
link(string prim_name) // function finds the linknumber based upon the name provided
{ // this will easily allow addition of primitives by simply unlinking, naming appropriately and relinking.
integer i = (llGetNumberOfPrims()+1);
integer a = 0;
for(a;a < i; a++)
{
string object = llGetLinkName(a);
if(object == prim_name)
{
llOwnerSay("link assigned to " + prim_name);
int_missing_link = a;
}
}
}
default
{
state_entry()
{
link("north_wall");
north_wall= int_missing_link;
link("south_wall");
south_wall = int_missing_link;
link("east_wall");
east_wall= int_missing_link;
}
touch_start(integer num_detected)
{
llOwnerSay("HEY");
llMessageLinked(north_wall, num_detected, "the root object has been clicked " + (string)num_detected +" times ", NULL_KEY);
llMessageLinked(south_wall, num_detected, "update_status, rotate", NULL_KEY);
}
Within the Child Prims(the other two walls ) put the following script
default
{
state_entry()
{
llSetText(llGetObjectName(),<120,120,120>,1.0);
}
link_message(integer n, integer no, string str, key q)
{
llOwnerSay(llGetObjectName() + " got the message " + str);
}
}
Result
The east_wall is the root prim. south_wall and north_wall are our two child primitives.
[4:11] east_wall: link assigned to north_wall
[14:11] east_wall: link assigned to south_wall
[14:11] east_wall: link assigned to east_wall
[14:11] east_wall: HEY
[14:11] south_wall: south_wall got the message update_status, rotate
[14:11] north_wall: north_wall got the message the root object has been clicked 1 times
Visit the Object here if you have access to GWU CITL
If you are ever linking many different primitives together and plan on having these primitives communicate with one another via linked messages, you may find it useful to have a logical method for tracking the object numbers. Perhaps using the objects name as its identifier is easier for the developer who may be adding objects to a primitive as they go. For the code, jump to the bottom.
How Linking Works
When an object is linked, each primitive within the object is assigned an integer as its unique identifier for the llMessageLinked(integer object_number, integer any_number, string your_message, key _avatarid). That first parameter of this function can be LINK_ALL, LINK_THIS, or a valid integer identifying a primitive in the object. How do you know the integer number? Second Life automatically assigns this integer number by making the last object clicked the integer "1" as its identifier, the second to last object clicked the "2", and you get the idea. The last primitive clicked(the root primitive) is the primitive whose position and size is shown when you right click and click edit from the selection wheel.
How to use this script
Below is the code you can put into a script above the default option. This will give you the option to use the private function
link("The Name of the Primitive You Want");
After this function is called. You will then have the global variable called int_missing_link, which can be used in a llMessageLinked command. This function allows me to never worry about a linknumber changing and having to click the objects in the right order to get the link numbers back to the appropriate number.
For the example below let us assume we have this small object with three primitives
The link command for the
Within the Root Prim put the following script
integer north_wall;
integer south_wall;
integer east_wall;
integer int_missing_link; // this is a global variable we want to know based on primitive name
link(string prim_name) // function finds the linknumber based upon the name provided
{ // this will easily allow addition of primitives by simply unlinking, naming appropriately and relinking.
integer i = (llGetNumberOfPrims()+1);
integer a = 0;
for(a;a < i; a++)
{
string object = llGetLinkName(a);
if(object == prim_name)
{
llOwnerSay("link assigned to " + prim_name);
int_missing_link = a;
}
}
}
default
{
state_entry()
{
link("north_wall");
north_wall= int_missing_link;
link("south_wall");
south_wall = int_missing_link;
link("east_wall");
east_wall= int_missing_link;
}
touch_start(integer num_detected)
{
llOwnerSay("HEY");
llMessageLinked(north_wall, num_detected, "the root object has been clicked " + (string)num_detected +" times ", NULL_KEY);
llMessageLinked(south_wall, num_detected, "update_status, rotate", NULL_KEY);
}
Within the Child Prims(the other two walls ) put the following script
default
{
state_entry()
{
llSetText(llGetObjectName(),<120,120,120>,1.0);
}
link_message(integer n, integer no, string str, key q)
{
llOwnerSay(llGetObjectName() + " got the message " + str);
}
}
Result
The east_wall is the root prim. south_wall and north_wall are our two child primitives.
[4:11] east_wall: link assigned to north_wall
[14:11] east_wall: link assigned to south_wall
[14:11] east_wall: link assigned to east_wall
[14:11] east_wall: HEY
[14:11] south_wall: south_wall got the message update_status, rotate
[14:11] north_wall: north_wall got the message the root object has been clicked 1 times
Visit the Object here if you have access to GWU CITL
Subscribe to:
Posts (Atom)