llSetText

From Second Life Wiki
Jump to navigation Jump to search

AKA "floating text" in the popular vernacular

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 llGetText function.
  • The floating text is a property of the prim and not the script, thus the text will remain if the script is deactivated or removed.
    • To remove floating text, one must assign an empty string with:

<lsl> llSetText("", <1.0, 1.0, 1.0>, 1.0); </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

You can push floating 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>

Useful Snippets

Removing Floating Text

Drag this script out of inventory onto an object to erase its set text: <lsl> // http://wiki.secondlife.com/wiki/llSetText default {

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

} </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 );