From: Forseti Svarog
how LSL's if...then...else works, {Snip} the difference between = and ==, dealing with variable types (if you need to convert a number to a string etc),
I tried a while back to teach an lsl class, and Ide love to keep doing it, But RL dose not permit me to keep a regular schedule with it.
First I realize some people reading this may not know what the data types are.
So heres a small description of the data types im using in this post.
integer, this is a data type that can store whole neg or pos numbers example -584 or 123456
float, can store numbers using a decimal example 10.508 or -0.005
vector, stores 3 floats, normally used to represent colors Red Green Blue values, Or 3D positions, Example <5.012, 200.221, 400.000>
string, stores just any line of text usually put in quotes, example "blablabla1234bla -db+_+)&"
There is a data type that lsl does not actually have called bool. Actually thats not surprising a lot of languages don't have it.
The bool data type stores a simple TRUE or FALSE value, nothing less nothing more.
Unlike strings, integers etc that can have a range of data.
But instead (like a lot of other languages) lsl Pretends to have a bool type by using integers.
Ie,, 1 or 0;
So you can say,
default{
state_entry(){
integer iFakeBool = TRUE;
llOwnerSay((string)iFakeBool);
}
}
and you will see
Object: 1The reason for putting (string) in front of it is because llOwnerSay needs a string type.
So we convert or "typecast" the integer iFakeBool to a string. That dosnt make iFakeBool a string permanently, just for that line.
"EDIT:" actually it dosnt MAKE iFakeBool a string,, Its just returning a String represntation of iFakeBool.
When we say integer iFakeBool = TRUE; Think of it like your telling the compiler,,
Store some data as an Integer using the Name iFakeBool, and make it equal TRUE/ or 1.
When you compare to things you use ==
example:
if(sumten1 == sumten2)
This actually makes a "boolean expression"
lets pretend for a minute that sumten1 and sumten2 both are integers and they both = 222.
You could say,
integer IsTheSame = sumten1 == sumten2;
We are saying,
Store some data as an Integer named IsTheSame, and make it equal the boolean result of (sumten1 == sumten2)
If you llOwnerSay((string)IsTheSame);
you will see " 1 "
(sumten1 == sumten2) equals TRUE or 1. if sumten2 was not the same lets say its 333,
then (sumten1 == sumten2) would equal FALSE or 0.
This makes for some interesting things in some scripts, Because where you would normally use an "if" statement you see something weird like,
integer ParrentLinkNum = (llGetLinkNumber() != 0);
Ok now to explain that line, First you need to understand something,
When working with Link messages, An object that has No other prims linked has a LinkNumber of 0. But If there are more than one prims in a link, the Parent prims number is 1.
Lets say you wanted to make a script that would always message the parent, regardless of what prim the script is in, Or if theres even other prims linked to it.
You could do something like this,
if(llGetLinkNumber() != 0){
llMessageLinked(1, SomeNUM, SomeSTRING, SomeKEY);
}
else{
llMessageLinked(0, SomeNUM, SomeSTRING, SomeKEY);
}
That would message linknum 1 If llGetLinkNumber() returns something other than 0.And if not it would message prim 0.
The only way llGetLinkNumber could return something other than 0 is if there are in fact more than 1 prims linked to the object.
But that is kinda to much, we should be able to do that in less code.
integer ParrentLinkNum = (llGetLinkNumber() != 0);
llMessageLinked(ParrentLinkNum, SomeNUM, SomeSTRING, SomeKEY);
Ok what we are doing here is another boolean logic operation,
!= means OtherThan equals, or Does not Equal.
So if llGetLinkNumber Does Not equal 0, then that statement is TRUE or 1.
if llGetLinkNumber equals 0 then that statement will return FALSE or 0.
We are saying,
store some data as an integer named ParrentLinkNum, and make it equal the boolean value of,
(does llGetLinkNumber() NOT Equal 0)thus if the script is in a prim that has no prims linked to it, ParrentLinkNum = 0;
But if the script is in a prim that has more prims linked to it,
ParrentLinkNum = 1;
Because the statement, (llGetLinkNumber does not equal 0), equals TRUE.We could actually further condense the code like this,
llMessageLinked((llGetLinkNumber() != 0), SomeNUM, SomeSTRING, SomeKEY);
Sense there is no actual bool type on lsl and it just stores booleans as integers 1 or 0 that line will work.
But if there was a bool type, llMessageLinked requires an integer for that first argument.
So you would need to do it this way.
llMessageLinked((integer)(llGetLinkNumber() != 0), SomeNUM, SomeSTRING, SomeKEY);
But don't worry about that, I just thought Ide throw that in there for anyone who may have wanted to know.
So the what about "if"s?
Well now that you understand booleans, or at least I hope you do.
You may realize that an if statement is nothing other than a light switch.
No mater how complex your if(blablabla) is, all your really ultimately saying is,
if(TRUE){
do this stuff;
}
and
if(FALSE){
this stuff is ignored;
}
integer SomeoneSaidWhoot;
if(SomeString == "Whoot"){
SomeoneSaidWhoot = 1;
}
else{
SomeoneSaidWhoot = 0;
}
is the same as
integer SomeoneSaidWhoot = (SomeString == "Whoot");
Bitfields..
An integer "number" is stored in memory using bit's "binary digit's".
Example ever seen the shirt that says this?
"there are 10 types of people in the world, those who understand binary and those who don't"
Without getting into deep discussion on it, the integers in lsl are 32 bit signed integers.
that means all numbers stored with integers use 32 bits regardless of the number size.
the number 0 as a 32 bit integer looks like this.
0000-0000-0000-0000-0000-0000-0000-0000
Im using the dashes for readability there not actually part of an integer.
the number 1 would be
0000-0000-0000-0000-0000-0000-0000-0001
and the number 2 would be
0000-0000-0000-0000-0000-0000-0000-0010
Now lets say there were only 2 bits.
That would be capable of storing 4 different combinations of on/off true/false logic.
00, 01, 10, 11
If we add a bit we go up to 8 possibility's
000, 001, 010, 011, 100, 101, 110, 111
4 bits would be able to do 16
0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111
ok theres a point to this.
With every added bit, the possibility's are multiplied by 2.
32 bit "signed" integers means one bit is used for the sign, ie to tell the difference between Negative and positive.
ok so, 32 bits could have...
No im not typing all that. 2,147,483,647 combinations with the 'Sign" bit set to 0, meaning positive numbers. And 2,147,483,648 with the sign bit 1, Negative numbers.
So 32 bit signed integer can have a whole number ranging from
-2,147,483,648 to 2,147,483,647
so theres 4294967295 possible combinations with 32 bits, "and one of them acting as a sign".
There are logical operators that havnt yet been discussed.
AND, OR, XOR, NOT
We will focus for now on AND, "&"
ok so 1 is
0000-0000-0000-0000-0000-0000-0000-0001
and 2 is
0000-0000-0000-0000-0000-0000-0000-0010
in code, (1 & 2) is 0
0000-0000-0000-0000-0000-0000-0000-0000
If the 2 compared bits are both true then the result is true.
& (AND)| FALSE TRUE
---------------------------
FALSE |FALSE FALSE
TRUE |FALSE TRUE
Well I think this post is getting a tad bit to long.
Hers the wiki link for Bitwise