Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

For begining and intermediate scripters- what confuses you the most?

Max Case
Registered User
Join date: 23 Dec 2004
Posts: 353
06-13-2005 14:47
I know for me, one of the greatest stumbling blocks when reading the wiki was wrapping my head around 'efficient' coding.
It's all the condensed logic that can cause problems for someone new to scripting.

so something like
if (mode++) llResetScript();
else llParticleSystem([]);

really threw me at first - I couldn't figure out how this was being used for a simple on/off switch for a particle system.

Until I realized, what was happening is that the first time the script encounters the IF, it checks MODE (and since it equals 0) it doesn't do the Reset. However, after it's done it's check, it adds one to MODE, so next time, MODE == 1, in which case it will be TRUE next time the if is encoutered, in which case it will Reset the script, resetting MODE to 0.

So sorry if thats more detail than wanted. ;)
So, in short, it is the condensed TRUE/FALSE logic that makes it harder for newbies. (or did for me in the beginning).
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
06-13-2005 16:44
From: Max Case
I know for me, one of the greatest stumbling blocks when reading the wiki was wrapping my head around 'efficient' coding.
It's all the condensed logic that can cause problems for someone new to scripting.

so something like
if (mode++) llResetScript();
else llParticleSystem([]);

really threw me at first - I couldn't figure out how this was being used for a simple on/off switch for a particle system.

Until I realized, what was happening is that the first time the script encounters the IF, it checks MODE (and since it equals 0) it doesn't do the Reset. However, after it's done it's check, it adds one to MODE, so next time, MODE == 1, in which case it will be TRUE next time the if is encoutered, in which case it will Reset the script, resetting MODE to 0.

So sorry if thats more detail than wanted. ;)
So, in short, it is the condensed TRUE/FALSE logic that makes it harder for newbies. (or did for me in the beginning).


Well, in my opinion, that's a piece of really bad code. Yeah, the compiler knows what to do, but even for a programmer, if you don't use that particular idiom all the time, you'll need to spend several minutes to figure out what its really doing.

I give a seminar where I work, titled "Writing Maintainable Code", which I feel is probably the most important (and overlooked) aspect of coding.

If you're going to write something like the above, you really need to put a comment in there to describe exactly what is happening and why, because its not immediately obvious. Even if you as the developer understand it completely, someone who is reading your code six months later might not.

- Jon
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
06-13-2005 17:38
From: Chosen Few
The LSL manual seems to be written exactly the same way. It tells you things like "All LSL scripts have a simple implicit state machine with one or more states. All scripts must have a default state, so iff there is only one state, it will be the ’default’ state. When a script is first started or reset, it will start
out in the default state."

Okay... What the heck is a "state machine", or for that matter a "state"?


A state machine can be thought of as a piece of code that has a single variable, the current "state", and a function for each state. A "state" is just what it sounds like -- running, walking, locked, unlocked, flying, swimming, etc. In a classic state machine, the program runs in a loop, and once per loop iteration it calls the function associated with the current state.

I'll give an example, because I use state machines for programming simple robot brains. One of my robots is a mini-sumo robot, which basically has to try and push another robot out of a ring (much like real sumo). The robot has a button, which the user presses at the beginning of a match.

When the robot turns on, the state machine is in the default state, which in this case is called 'waitingForButtonPress'. The function associated with that script is called 100 times per second, and all it does is check the condition of the input pin that is connected to the button. When the user presses the button, the state machine switches to the next state, which is 'pause'. After the user presses the button, the robot has to stay still for five seconds before moving. The pause state handler function gets called 100 times per second, and all it does is increment a counter. When the counter reaches 500, the function switches to the next state.

With LSL, things are slightly different, because it doesn't call your state handler constantly. LSL uses events, which can include things like timers, keyboard press & release, rezzing the object the script is in, receiving linked messages, listening to chat, etc. Each state has one or more event handlers, which are functions that are called when that event happens.

Each state can have its own set of event handlers. When a state is the current one, events that happen trigger the event handers in that state.

The state an LSL script starts in is always "default". So, if you get an event when the script has just started or been reset, the event handlers in the default state are the ones being called.

I hope this helps -- if you have more questions, or don't understand what is clear about this, please reply, and I'll try and do my best to do a knowledge dump.

- Jon
Catherine Cotton
Tis Elfin
Join date: 2 Apr 2003
Posts: 3,001
06-13-2005 17:49
for me the discriptions of the funtions and such on the wiki are hard to understand. It feels for me at least those are written as if they are speaking to a programer. Maybe a wiki for none programers would help? dunno

arabic was easier to learn :D

Cat
_____________________
Catherine Omega
Geometry Ninja
Join date: 10 Jan 2003
Posts: 2,053
06-13-2005 17:54
That's a good explanation, Jon. Can I copy that to the wiki? I can attribute it, if you like.
_____________________
Need scripting help? Visit the LSL Wiki!
Omega Point - Catherine Omega's Blog
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
06-13-2005 18:28
From: Catherine Omega
That's a good explanation, Jon. Can I copy that to the wiki? I can attribute it, if you like.


Sure, feel free...

- Jon
Brace Coral
Basic Account Crew
Join date: 11 May 2004
Posts: 666
What Confuses me the most?
06-13-2005 20:57
How the tutorials, wiki and whatever else "how to script" and "scripting help" are written. Chosen Few expressed it perfectly. The other day I tried AGAIN to read the LSL manual after my first go a year ago. Still Greek to me.

Like Brace After Relog from llTeleportAgentHome When Flying to Event.
basically confused angry and back to where I had started from.

Yep a wiki written for those of us who would love to learn scripting and simply need things explained in plain English with the supposition that we DON'T know anything about coding or have had any experience in any other coding languages.

Thanks Jon for the wonderful explantion! THATS what I'm talkin bout :)

From: Chosen Few
Couldn't have said that better myself, Chip. Every time I attempt to read the docs, all I can think is "Darmock and Jalad at Tenagra".

In case anyone doesn't get that reference, I'll explain it (unlike what would happen if this post were a scripting manual). In one of the more interesting episodes of Star Trek TNG, Picard finds himself faced with the daunting task of having to communicate with the Tamarians, a race who frustrate everyone they encounter because they appear to make no sense whatsoever. It turns out their entire language is based on anecdote. They speak not in sentences, but in odd phrases from stories that the listener most already know in order to get the meaning of each phrase. They say things like, "Darmock and Jalad at Tenagra", and "Shaka, when the walls fell," and "Temba, his arms wide," fully expecting that the listener already knows what those things mean, and never even dreaming that someone might not.

The LSL manual seems to be written exactly the same way. It tells you things like "All LSL scripts have a simple implicit state machine with one or more states. All scripts must have a default
state, so iff there is only one state, it will be the ’default’ state. When a script is first started or reset, it will start
out in the default state."

Okay... What the heck is a "state machine", or for that matter a "state"? I have no idea. The word was used no less than six times in this single paragraph (if you can call it a paragraph), but the book has offered no explained what it means (we're only 3 paragraphs past "You're probably wondering what you can do";). So, I flip back to the table of contents and find there's a section called "States". I turn to that section only to find it's just as abstruse as the the page I came from. It sas, "All scripts must have a ’default’ state, which is the first state entered when the script starts. States contain event handlers that are triggered by the LSL virtual machine. All states must supply at least one event handler - it’s not
really a state without one."

So now I'm left to wonder not only what is a state (the word has been used 5 more times, but still no definition), but also what is an event handler and a virtual machine. All I'm able to gather so far is that the authors absolutely love the word "state", and that it has no defintion of what it is, only what it sometimes is not, as a state without an event handler is not really a state (which could explain New Jersey I suppose, but doesn't really help me here.)

At that point I gave up, or as the Tamarians might put it, "Chosen Few, his face black, his eyes red."

That was a year and a half ago, and I'm no closer to unlocking the two great mysteries of the scripting world. The first of course is what is a state, and the second is how does one get past page 3 of the LSL manual without having visions of torturing small animals. All I'm able to do is more or less what Azazel said. I grab an existing script, fiddle with it until I figure out what to change so it will do what I want, and hope for the best. Most times, I end up breaking it, and then calling in one of my more feted friends to put it back together.
_____________________
LL Brokted my Sig

From: Pol Tabla
I love Brace Coral.

Just sayin', like.
Toneless Tomba
(Insert Witty Title Here)
Join date: 13 Oct 2004
Posts: 241
Thinking back....
06-13-2005 23:15
I remember these topics was hard for me to grasp:

llMessageLinked()
States
Most loops
Sensors

In the beginning I took a intro to scripting class offered at TLC first. Showed me just enough to be a little dangerous, but mostly I started taking apart freebie scripts and hacking them to make them do what I wanted it to do or break it trying. Yadni's Junkyard & the Scripting Library on the forums was a God send. Little by little I stopped hacking scripts and started to write them on my own. My learning curve took me about 4 months to be semi-decent.
Alexa Hope
Registered User
Join date: 8 Dec 2004
Posts: 670
06-14-2005 02:11
Scripting could come from an alien planet for all I know. With no programming experience at all, when I attended a beginners scripting class it all went straight over my head. With step by step instructions I made a cube say 'hello Alexa Hope', after which my understanding went downhill and I left the class. The LSL help document is written in an unknown foreign language yet to be translated.

But, I am going to try the suggestion some of you made about taking a ready made script and play with it to see if I can make any progress at all. I doubt this would lead me to writing my own scripts but we shall see.

Maybe after 6 months of playing, I will read the scripting forums for the first time.

Alexa Hope
Olympia Rebus
Muse of Chaos
Join date: 22 Feb 2004
Posts: 1,831
06-14-2005 07:57
From: Brace Coral
How the tutorials, wiki and whatever else "how to script" and "scripting help" are written. Chosen Few expressed it perfectly. The other day I tried AGAIN to read the LSL manual after my first go a year ago. Still Greek to me.

Yep a wiki written for those of us who would love to learn scripting and simply need things explained in plain English with the supposition that we DON'T know anything about coding or have had any experience in any other coding languages.



Maybe the wiki should have a "any questions" box at the bottom of each entry. Then the contibuters could see the newbie snagging trouble spots and address them in page updates.

Here's a recent wiki puzzler:
From: someone
vector llVecNorm(vector v)
This function returns the normalized (a.k.a. unit) vector representing v. This vector can be multiplied (scalar) by llVecMag(v) to get v again.
Since vectors are used to represent direction as well as location, this function returns the 'simplest form' of the vector, otherwise known as the direction of the vector.

Normalizied?
Unit?
Something about the simplist form and direction? Wha...?

I eventually figured out that this function took a vector that said "point thataway for this long" and simplified it to "point thataway". Once it clicked, it seemed so obvious.
Perhaps the standard wiki explanations should include a "I still don't know what you're talking about" link to explain stuff in plain English (or baby talk).
_____________________
Ananda Sandgrain
+0-
Join date: 16 May 2003
Posts: 1,951
06-14-2005 08:03
Actually Catherine, that explanation of a state is still not basic enough. At the end of it, I'm STILL thinking, "Um, so what is a state?"

It's the words. You're still assuming that people know the specialized meanings of the words, or for that matter, even the general english meaning of the words. So, let's get out the dictionary:

state n. 1. A condition or mode of being with regard to a set of circumstances; position

From this I would infer that a state in a script is a continuing beingness that the script is assuming.

Now, from the point of having the basic definition, can anyone tell me what might be a specific definition as applies to scripting?
_____________________
Max Case
Registered User
Join date: 23 Dec 2004
Posts: 353
06-14-2005 08:25
From: Olympia Rebus
Maybe the wiki should have a "any questions" box at the bottom of each entry. Then the contibuters could see the newbie snagging trouble spots and address them in page updates.


That's kinda what I use the comments area on the wiki for. I think part of the problem is that people are afraid to ask potentially 'stupid' question in a public forum.

Ananda:
Re: What is State Machine: One way I have heard it described (dont remember where) is that it's like a gear shift in a car. You have Neutral, Drive, Reverse. Each of those is a 'state' and the machine (car) can only do certain things in that state. ie: you can only drive backwards in the reverse state.
Of course, if you have never driven, that maybe doesn't help. If that's the case, get the to a Video Arcade and try to drive :)
Jarhyn Wilde
Registered User
Join date: 11 Aug 2004
Posts: 41
06-14-2005 10:11
Anyone up for a moderate to advanced scripting session? The only thing that isn't fair game is vehicles. Everything else I can either explain and demystify, or figure out on the spot with you together. I always wanted to do a scripting class for people who already know the very basics. If you are interrested in setting up some sort of event, though, I need a place to do the event and a reasonable interest in it. It is going to be an open discussion session, and therefore not exactly a class with a curriculum. Everyone has different things they don't understand and each would be taken in turn.
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
06-14-2005 10:27
From: Jarhyn Wilde
Anyone up for a moderate to advanced scripting session? The only thing that isn't fair game is vehicles. Everything else I can either explain and demystify, or figure out on the spot with you together. I always wanted to do a scripting class for people who already know the very basics. If you are interrested in setting up some sort of event, though, I need a place to do the event and a reasonable interest in it. It is going to be an open discussion session, and therefore not exactly a class with a curriculum. Everyone has different things they don't understand and each would be taken in turn.


If my schedule allows, I'd be willing to sit in and help answer questions as well.

- Jon
Catherine Omega
Geometry Ninja
Join date: 10 Jan 2003
Posts: 2,053
06-14-2005 13:30
From: Ananda Sandgrain
Actually Catherine, that explanation of a state is still not basic enough. At the end of it, I'm STILL thinking, "Um, so what is a state?"

Now, from the point of having the basic definition, can anyone tell me what might be a specific definition as applies to scripting?
A state can be thought of as a "state of being". Think of an ordinary car tire. It could have several states of being, the key ones being "full of air" and "flat". Depending on which state it's in, doing different things to it will cause different results to occur.

For example, suppose we take a flat tire and try pumping air into it. Assuming no holes, it's going to fill up. If we take an already full tire and do the same thing, we're likely either going to hurt ourselves, or break the tire. In LSL, states are the same way. Each state describes a predetermined way for your script to behave while in that state. The only difference between states in LSL and the tire is that in LSL, you can effortlessly switch from one to the other.
_____________________
Need scripting help? Visit the LSL Wiki!
Omega Point - Catherine Omega's Blog
LordJason Kiesler
imperfection inventor.
Join date: 30 May 2004
Posts: 215
06-14-2005 18:33
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,
CODE

default{
state_entry(){
integer iFakeBool = TRUE;
llOwnerSay((string)iFakeBool);
}
}

and you will see
Object: 1
The 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,
CODE

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.
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,
CODE

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

CODE

integer SomeoneSaidWhoot;
if(SomeString == "Whoot"){
SomeoneSaidWhoot = 1;
}
else{
SomeoneSaidWhoot = 0;
}


is the same as
CODE

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
_____________________
"no, my alt is clean on crashing any sims"
1 2