llSetText

From Second Life Wiki
Jump to navigation Jump to search

AKA "floating text" in the popular vernacular. This creates text that floats / hovers above the prim.

To actually display text on a prim, see XyzzyText, or consider using parcel prim Category:LSL_Media options (useful only you have control over the land's media settings.)

Summary

Function: llSetText( string text, vector color, float alpha );
0.0 Forced Delay
10.0 Energy

Displays text over a prim with specific color and transparency (specified with alpha).

• string text text to display between the quotes
• vector color color in RGB <R, G, B> (<0.0, 0.0, 0.0> = black, <1.0, 1.0, 1.0> = white)
• float alpha from 0.0 (clear) to 1.0 (solid) (0.0 <= alpha <= 1.0)

Caveats

  • A script calling llSetText cannot know if it is or is not changing the floating text, since there is no mirror llGetText function;
  • Nor, for the same lack of the "missing" llGetText function, can a script know what text is hovering about a prim;
  • The limit on the character length of floating text is 255 characters;
  • Floating text will go right through walls or any other object. Be considerate of neighbours in malls and apartment buildings.


Removing Floating Text

Floating text is a property of a prim and not a script. For that reason, the text will remain if the script is deactivated or even removed.

To remove floating text, you don't actually remove it, but rather swap in an empty string like this: <lsl> llSetText("", <1.0, 1.0, 1.0>, 1.0); </lsl>

Here is a complete script to erase the floating text of an object. You can just make the script in inventory, replace its contents with the script text below, and drag this script into the contents of the object:

<lsl> default {

   state_entry(){
       llSetText("", <0.0, 0.0, 0.0>, 0.0);
       llRemoveInventory(llGetScriptName());
   }

} </lsl>


Font face and size

Apart from colour, we don't have a lot of control over how floating text will appear.

You cannot, for instance, change the font, or the size of the font.

Nor can you align it to right or left. It will always be centred horizontally above the prim.


Height

Floating text floats straight up on the Z-axis (the world's, not the prim's) from the centre of the prim (that contained the script that called it) to be above the prim at a height equivalent to half the prim's Z scale (plus a bit more.)

You can push floating text up higher above the prim by doing something like this:

llSetText("Monkeys\n\n\n\n\n ", <1.0, 1.0, 1.0>, 1.0);

The \n 's stand for newlines, so in the above example, the floating text will be 5 lines higher than normal above the prim.

Note the space after the last \n : pure vertical space is auto-trimmed from the end of a floating text string. You trick the system into leaving it alone by adding that one space.


Wrapping

By default, any floating text will appear on a single line. Sometimes these single lines of floating text can be absurdly long.

However, the floating text can be spread over multiple lines by inserting line breaks ("\n").

You can do this manually, or by using functions such as SplitLine (which requires separators to be in the text), or WrapText (which does not require separators in the text and allows you to specify the line-length you feel best.)

Examples

Example: Manual multiple line <lsl> llSetText("I am \n on two lines!", <0.0, 1.0, 0.0>, 1.0); </lsl>


Example: Including llSetText in default code to show object's name in green text: <lsl> default{

    state_entry() {
         llSay(0, "Hello, Avatar!");
         llSetText(llGetObjectName(), <0.0, 1.0, 0.0>, 1.0); // Display object's name in green
    }

    touch_start(integer total_number){
         llSay(0, "Touched.");
    }
}

</lsl>


Example: Use prim name as the text, wrapped to display on several lines

<lsl>

string WrapText(string pcText, integer piWidth) {

   list     laLines  = [];
   integer  liIndex;
   integer  liKeep;  // Specifies if we keep the char pointed at or not
   integer  liLen    = llStringLength(pcText);
   list     llSearch = [" ", "\n"];
   
   while (liLen > 0) {
       liIndex = piWidth;
       if (!(liKeep = (liLen <= piWidth))) {
           while ((liIndex >= 0) && (-1 == llListFindList(llSearch, (list)llGetSubString(pcText, liIndex, liIndex))))
               --liIndex;
           if (liIndex <= 0) {
               liIndex = piWidth;
               liKeep = 1;
           }
       }
       laLines += llGetSubString(pcText, 0, liIndex - 1);
       pcText = llDeleteSubString(pcText, 0, liIndex - liKeep);
       liLen -= (1 + liIndex - liKeep);
   }
   return llDumpList2String(laLines,"\n");

}


default {

    state_entry() {
         string tmp = WrapText(llGetObjectName(),25);
         llSetText(tmp, <1.0, 1.0, 1.0>, 1.0); // Display object's name in solid white
    }
}

</lsl>


Example: Colours

<lsl> vector white = <1.0, 1.0, 1.0>; vector red = <1.0, 0.0, 0.0>; vector green = <0.0, 1.0, 0.0>; vector blue = <0.0, 0.0, 1.0>; vector grey = <0.5, 0.5, 0.5>; vector black = <0.0, 0.0, 0.0>; </lsl> <1.0, 1.0, 1.0> represents the values for red, green, and blue. <1.0, 1.0, 1.0>, means "white" and <0.0, 0.0, 0.0> means "black": <lsl> llSetText("I am on", <1.0, 1.0, 1.0>, 1.0); </lsl> <lsl> llSetText("I am off", <0.0, 0.0, 0.0>, 1.0); </lsl>

Example: Alpha (transparency)

The 1.0 is the alpha setting. 1.0 means fully opaque (aka solid), and 0.0 would be completely transparent (invisible): <lsl> llSetText("alpha", <0.0, 1.0, 0.0>, 0.5);

</lsl>

See Also

Articles

•  Color in LSL
•  Translucent Color
•  Examples: SplitLine Insert 'new line' escape codes at certain positions of a string
•  Useful snippet: llGetObjectPermMask Label an object with text and newlines to give away or sell

Deep Notes

Signature

function void llSetText( string text, vector color, float alpha );