User:SuzannaLinn Resident/ScriptingClasses/Floating Texts

From Second Life Wiki
< User:SuzannaLinn Resident‎ | ScriptingClasses
Revision as of 10:45, 29 October 2024 by SuzannaLinn Resident (talk | contribs) (Created page with "= Floating Texts = == Monday class == === Add a floating text to a box === -> Object 1 Floating Text on box (a red box) -> Script 1 Floating Text on box The script starts...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Floating Texts

Monday class

Add a floating text to a box

-> Object 1 Floating Text on box (a red box) -> Script 1 Floating Text on box

The script starts with a list of color names, in lines 0-16. Let's start explaining how the colors work.

A color is defined with 3 values with decimals, between 0 and 255, for the Red, Green and Blue components of the color. To keep the three RGB values we use the type "vector".

Vectors are often used for the position of the object, for the values of the x, y and z coordinates. Also for the size of the object, the height, width and depth coordinates.

And since we also use 3 values to define colors , the scripting language uses the same type vector for the colors (instead of creating a type "color").

We are creating a variable for each color. A variable of type "vector". And we are assigning a value to the variable immediately.

As you see in the script, the 3 values are separated by commas between < and >. This is the way to assign values to a vector type variable.

But, although RGB values are in the range from 0 to 255 everywhere in the world, lsl script uses a range from 0 to 1, so most of them have decimals.

For instance, in line 0, we are creating the variable NAVY, of type "vector", and assigning to it the value <0.000, 0.122, 0.247>.

We are using a different naming style for the variables in lines 0-16, all in uppercase.

It's a naming convection to show that the value of this variable wil be constant along the script. We are assigning the value once at the start and never changing it.

Other programming languages have a way to create constant named values. LSL has not. So we use variables with all uppercase names.

If the name of the "constant" variable has several words, we will use an underscore _ to separate the words, like MY_CONSTANT.

Now let's go to write a floating text on our box.

The function that write the floating text is llSetText() and it has 3 parameters: the text to display, the color of this text, and the alpha.

The parameter "text" is of type string. The parameter "color" is of type vector, as we have just seen. And "alpha", that can have decimals, is of a type for numbers with decimals, the type "float".

We can have the text in several lines using "\n" which means new line, for instance:

  • "Hello world \n I'm here!"

The alpha is the transparency of the text. Its values are between 0.0 and 1.0.

1.0 is completely opaque, 0.0 is transparent and invisible, 0.5 is half transparent, etc.

Since floating texts are sometimes difficult to read we will use 1.0. You can try later with other alpha values to see how it looks.

In the event state_entry, lines 24-29, we initialize the variables for the three parameters.

For the text we will display our name. Here we use two functions.

The function llGetOwner() returns the UUID of the owner of the object where the script is in.

The function llGetDisplayName() takes an avatar UUID and returns the name.

And finally, in the event touch_start, lines 31-34, we have llSetText() to set the floating text.

Well, we could have just scripted this:

default {
    state_entry() {
    }
    touch_start(integer num_detected) {
        llSetText("Hello world!",<1,0,0>,1.0);
    }
}

which does about the same, but i think that it's more fun to be learning more things along the way.

Now touch your box, to see the floating text appearing.


Make the text change colours every half second

-> Object 2 Floating Text changing colors (a yellow box) -> Script 2 Floating Text changing colors

This script has the same code as the previous one with some more lines added.

To make the floating text change color from time to time, we need to control the time.We will get it using the event timer and the function llSetTimerEvent().

The event "timer" is triggered regularly with an interval of seconds. If the timer is set, for instance, to 10 seconds, every 10 seconds our script will receive and event timer telling us that it's time to do something.

We set the interval with the function llSetTimerEvent(), in line 36.

The function llSetTimerEvent() has one parameter: after how many seconds we want the timer event triggered.

We want to change the color every half second, so we will use llSetTimerClock(0.5), and the event timer will be triggered every half a second.

To know which color to change to, we are using a counting variable, colorCounter, thats goes from 1 to 3, increased in each timer, and when arrives to 4 is changed back to 1, in lines 41-57:

colorCounter = colorCounter + 1;
if (colorCounter>3)
    colorCounter=1;

And for each value of colorCounter we assign a color. Later as homework, you can change the colors and use 4 or more colors instead of 3.

If we wanted the event timer to stop being triggered, we would use llSetTimerEvent(0). Setting the timer to 0 seconds stops it.

Now touch your box, to see the floating text changing colors.


Option to type a new text in chat

-> Object 3 Floating Text change text (a green box) -> Script 3 Floating Text change text

We will change the floating text typing a new text in chat. So we will need a way to get that the script knows what we are typing. And we will like that what we type doesn't appear in the public chat, because it's just a private conversation between us and our scripts.

We need to open a private channel of comunication. We will type our new text in this channel and our script will be listening in the same channel.

Channels have numbers. The channel number 0 is the public chat. Our chat goes to channel 0.

We can choose any number for our channel, up to 9 digits. In this script we will use channel 11.

We can type in a channel starting with a "/" and a number of channel, for instance:

  • "/11 I'm typing in channel number 11" (without the quotes, of course)

And if we type this, nobody and nothing will see it, unless they are listened to this channel.

Let's see how we will make our script to listen.

The function is llListen(), in line 39. It has 4 parameters:

  • the channel we want to listen to (number 11, in our example).
  • the user name of the person we want to listen to.
  • the UUID of the person, we will use this parameter or the user name parameter, not both.
  • the message, if we are listening for a specific message.

We have to choose a channel to listen, the other parameters are optional, and we will use them to filter the message.

In this case we will listen to channel 11, and only to messages sent from ourselves. Otherwise we will be listening to the messages from the other students, who will be using also channel 11 to change their floating texts:

  • llListen(11,"",llGetOwner(),"")

We are again using llGetOwner(), to get our UUID.

And what happens when the script received a message from us to the channel 11? It triggers the event listen, which has the same parameters than the llListen function.

In the event listen. in lines 65-72, is where we will change the floating text.

Let's look at our script to see what we have added to the previous script.

At the start, after the colors, in line 18, there is:

  • integer CHANNEL_FLOATING_TEXT=11;

We could have used the number 11 in the listen function and event, but it's better to use a constant with a nice name or it would get very confusing if we listen to several channels.

In the event touch_start, lines 34-40, we have the llListen() function.

And at the end of the script we have added the event listen, lines 65-72.

You see that in the parameters of the listen event, the 3rd parameter, which is the UUID, has the type "key".

The type key is used for UUIDs of any kind: avatars, objects, textures, notecards,... it could have been called type "uuid" but it's named key.

Inside the listen event we check that the channel is the our CHANNEL_FLOATING_TEXT. No need to do this here. We are only listening to a channel and we will not receive messages from any other channel. But it's better to do it, in case that we add more channels to our script in the future.

And we change the text variable to what we have typed in channel 11. And... don't we use the llSetText() function to change it?

No need, we have a timer that sets the text every half a second.

Now touch your box, and say a new text on channel 11, to see the floating text changing.


Add a second text and make the texts change every 15 seconds

-> Object 4 Floating Text two texts (a light blue box) -> Script 4 Floating Text two texts

Now we want to have two texts and the floating text will change from one to the other every 15 seconds, and they will stay changing colors.

Do we add a second channel to listen for the second text message? Yes, we will do this.

And do we add a second timer set to 15 seconds? No, this is not possible!

There is only one timer. If we set it to 15 seconds, it will stop being triggered every half second, and our floating texts will change colors very slowly, every 15 seconds.

We need another way to get it.

We will set the timer at the shorter time, 0.5 seconds. And we will have a variable that counts the timer events. Our 15 seconds "timer" is 30 times the 0.5 seconds timer. So we will count 30 timer events before changing between the two floating texts.

Let's look at our script to see how we are doing this all.

At the start we have added a second channel and changed the name of our previous one, in lines 18-19: integer CHANNEL_FLOATING_TEXT_1=11; integer CHANNEL_FLOATING_TEXT_2=12;

Right after it, we have added new variables:

  • text1 and text2, for the two floating texts.
  • currentText to know what text, 1 or 2, is currently displayed.
  • textTicks is the counter of 0.5 timer events, to make our 15 seconds "timer".

At the event state_entry, lines 32-40, we have added:

  • the text goes to the variable text1 and a new text goes to text2, the function llGetUsername() takes a UUID (a type key variable) and returns the username.
  • the variable is set to text1 to start displaying this text, and currentText is set to 1.

A detail about the functions llGetDisplayName() and llGetUsername(): DisplayName has upper N, and Username has lower n.

At the event touch_start, lines 42-50, we have added:

  • the listen() function for the second channel, and changed the name of the first channel.
  • the textTicks to start at 30, it will go down by 1 at each timer event, and change text when it arrives to 0.

At the event timer, lines 52-89, we have added:

  • the textTicks decreases by 1.
  • when the textTicks arrives to 0, it changes the text, and sets textTicks back to 30.

At the event listen, lines 91-106, we have added:

  • we are listening to the second channel, and changed the name of the first channel.
  • the current text is set to the other one, and textTicks are set to 1, so in the next timer the text will be changed to the new one.

Now touch your box, and say two new texts on channels 11 and 12, to see the text changing.


Use the text on ourselves

-> Script 5 Floating Text on yourself

We can't add a floating text to avatars, only to objects.

The way to have one floating text on us is adding an object with the floating text. We want the text to be over our head so we will attach this object to the skull.

And we don't want the object to be visible, so we will make it transparent.

The function to change the transparency is llSetAlpha. To make our object transparent we will use:

  • llSetAlpha(0.0,ALL_SIDES)

0.0 is the value of the alpha (the transparency), it's the same value that we have seen in the 3rd parameter of the llSetText function.

ALL_SIDES is a predefined constant that refers to all the faces of the object.

In the script there are only 2 changes:

  • we have added the llSetAlpha function in the event state_entry, in line 34.
  • we have moved the lines in the event touch_start event to the event state_entry, the floating text will appear as soon as we add the object without touching it.

Now look at your Class Materials folder and add the object "Floating Text on yourself", to see the floating text on you. You can change the texts using channels 11 and 12.

If you want the floating text to be shown higher or lower you can edit the object size while wearing it. The bigger the object the higher the floating text displays.


Answers to questions

Why color black isn't just <0,0,0> ?

  • It's a good idea to avoid using <0,0,0>, pure black, when representing colors because even nearly black reflects some light. If our object reflects no light at all, we will lose details that add to the realism and look of your object.

Is different the transparency in the floating text and the transparency in the editor?

  • In the scripts the transparency (alpha) goes from 1 (fully opaque) to 0 (fully transparent), with decimals.
  • In the editor it goes from 0 (fully opaque) to 100 (fully transparent), without decimals.

How does the line break to show several lines in the floating text?

  • The \n is the new line character.
  • We can have a floating text with several lines adding \n between the lines.
    • For instance, if we get the data in a loop, we can add it to a variable like this:
      • text = text + line + "\n";

What is changed(interger change)?

  • changed, in line 66, is an event.
  • The event changed is triggered when something related to the object changes.

^^ in the script we are looking if the owner has changed, or the inventory has changed, to reset the script.

Is the event changed triggered when we edit a notecard?

  • When we edit a notecard in an object and save it, the old notecard is replaced by a new one with the same name and this counts as a change in the object.
  • This means that if we have a configuration notecard that the script reads on initialization, all we have to do to make it update is watch for CHANGED_INVENTORY in the event changed and reset the script at that point, so it will read our updated configuration.
  • The event changed is triggered by changes external to the script. Changing the floating text doesn't trigger it.

In the code in the event timer, could we, instead of four if conditions, use only one if and thre else if conditons, so the script would not go through all of them if one TRUE is found?

  • Yes, very right, we can use "else if" to optimize the code.

If we delete the script, will the text keep animating?

  • Deleting the script, the text will stop changing color, but the text will stay, in the color that it had at the moment of deleting the script.
  • The floating text is associated with the object, not with the script, so deleting the script doesnt remove the floating text.
  • To remove the text we need set it to empty:
    • llSetText( "", <0,0,0>, 0);

Could we listen to 3 channels instead of 2?

  • Yes, we can listen up to 64 channels, but better to use only a few.

What is a scrolling floating text?

  • There are two ways of scrolling:
    • A very long line, where we show only a part of it, and the part shown is moving one charracter to the left with a timer.
    • Or many short lines, where we show only some of them, and the part shown is moving one line up with a timer.