Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Tell me about typecasting, plz.

Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
07-19-2006 04:34
You can change color when you said like, "<1.0, 0.0, 0.0>" by the following script.
CODE
default
{
state_entry()
{
llListen(0, "", "", "");
}
listen(integer channel, string name, key id, string message)
{
llSetColor((vector)message, ALL_SIDES);
}
}

Now, you can change color (to red) when you said anything by the following script.
CODE
default
{
state_entry()
{
llListen(0, "", "", "");
}
listen(integer channel, string name, key id, string message)
{
llSetColor((vector)"<1.0, 0.0, 0.0>", ALL_SIDES);
}
}

And this is also available.
CODE
string Red = "<1.0, 0.0, 0.0>";

default
{
state_entry()
{
llListen(0, "", "", "");
}
listen(integer channel, string name, key id, string message)
{
llSetColor((vector)Red, ALL_SIDES);
}
}

But why is it unavailable even when I says, "Red" by the following script?
CODE
string Red = "<1.0, 0.0, 0.0>";

default
{
state_entry()
{
llListen(0, "", "", "");
}
listen(integer channel, string name, key id, string message)
{
llSetColor((vector)message, ALL_SIDES);
}
}
_____________________
:) Seagel Neville :)
Ginge Reymont
Registered User
Join date: 10 Oct 2005
Posts: 190
07-19-2006 05:19
CODE

Vector Red = <1.0, 0.0, 0.0>;

default
{
state_entry()
{
llListen(0, "", "", "");
}
listen(integer channel, string name, key id, string message)
{
if (message == "Red")
{
llSetColor(Red, ALL_SIDES);
}
}
}
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
07-19-2006 05:45
:D Thanks. But what I wanted to know why it was unavailable.
If it were available, I would not have to put down a hundred of if conditions to achieve that. ;)
_____________________
:) Seagel Neville :)
Bitzer Balderdash
Dazed and Confused
Join date: 21 Dec 2005
Posts: 246
07-19-2006 06:04
you are mixing up typecasting with inline evaluation or variable redirection.

Typecasting means to treat a number as a string, or a string as a vector, it does not allow you to use that value of a variable whose name is the value of another variable.

for example
CODE

string red="<1,0,0>"; // sets the variable called "red" to the literal string "<1,0,0>"

string color = "red"; // sets the variable called "color" to the literal string "red"

string vec = red; // sets the variable called "vec" to the value of the variable called "red"

Do you see the difference between the second and third ones?

In your example, you have a variable called "message" which is set to the literal string "Red", which is what you typed. That is not the same as setting it to the value of the variable which happens to be called "Red".

If you want to minimise your if clauses, do something like
CODE

list colors = ["red", <1.0, 0.0, 0.0>,
"green", <0.0, 1.0, 0.0>,
"blue", <0.0, 0.0, 1.0>];

default
{
state_entry()
{
llListen(0, "", "", "");
}
listen(integer channel, string name, key id, string message)
{
integer pos = llListFindList(colors, message);
if (pos != -1)
{
llSetColor(llList2Vector(colors, pos+1), ALL_SIDES);
}
}
}
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
07-19-2006 06:35
From: Bitzer Balderdash
In your example, you have a variable called "message" which is set to the literal string "Red", which is what you typed. That is not the same as setting it to the value of the variable which happens to be called "Red".
Did you say whenever I said, "Red", message turned to be the literal string "Red", not the variable called "Red"? Aha, I'm being confused a bit, but I feel that I'm getting it. Thank you. :)
_____________________
:) Seagel Neville :)
Llauren Mandelbrot
Twenty-Four Weeks Old.
Join date: 26 Apr 2006
Posts: 665
You got it!
07-19-2006 10:33
From: Seagel Neville
Did you say whenever I said, "Red", message turned to be the literal string "Red", not the variable called "Red"?
Correct!
From: Seagel Neville
Aha, I'm being confused a bit, but I feel that I'm getting it. Thank you. :)
You are indeed learning. keep up the good work!

Toodle-oo!