Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Sloppy Scripting?

Azzura Supplee
Safety Girl
Join date: 16 Jun 2006
Posts: 29
09-12-2006 08:59
Im not a programer - so Im learning scripting by messing with SL objects

I just wrote this script to control an arrow that slides around a picture to point at things as I teach. - Im posting this to see if I am being sloppy and inefficient. Just dont want to teach myself bad habits! Thanks for your comments ahead of time!

CODE

integer clicks;
list positions = [<246,25.844,90>,<246,27,96.250>,<246,30.118,95.867>,<246,27,97.768>,<246,27,96.250>];


moveit()
{

if (clicks + 1 > llGetListLength(positions)) //reset if you hit the end of the list
{
llSetPos(<246,25.844,90>);
clicks = 0;
}

if (clicks > clicks - 1)
{
llSetPos(llList2Vector(positions,clicks));
}

}

default
{
state_entry()
{
llListen(2,"","","reset");
}

listen(integer chan, string name, key id, string mess)
{
if (llToLower(mess) == "reset")
{
llSetPos(<246,25.844,90>); //reset to start position
clicks = 0;
}
}

touch_start(integer num_detected)
{
clicks ++;
moveit();
}
}
Vares Solvang
It's all Relative
Join date: 26 Jan 2005
Posts: 2,235
09-12-2006 11:21
I am new to scripting as well, but I did see one thing. The style guide in the LSL wiki says that clicks ++; is slower that using clicks = (clicks + 1);

It won't matter in a small script like this one, but might be important in a larger more complex script.
_____________________
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
09-12-2006 11:26
++clicks;
is going to be just as fast as
clicks = clicks + 1;
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Vares Solvang
It's all Relative
Join date: 26 Jan 2005
Posts: 2,235
09-12-2006 11:28
From: Strife Onizuka
++clicks;
is going to be just as fast as
clicks = clicks + 1;



Heheh, I just noticed that. =)

Here is a link to the style guide in the wiki mirror Azzura, if you want to look at it. Some good stuff in there:

http://rpgstats.com/wiki/index.php?title=StyleGuide
_____________________
Pale Spectre
Registered User
Join date: 2 Sep 2005
Posts: 586
09-12-2006 11:35
I don't see anything particularly sloppy.

My main concern would be the use of llSetPos with what look like region co-ordinates... if you move your picture, your pointer might disappear over the horizon. :) If you make sure the picture is the Root Prim, and have your pointer linked to the picture, then llSetPos will move the pointer relative to the picture... but you will need to calculate new vector postions that are relative to the picture.

I would also remove the repetative reference to positons(0). And, maybe restrict the use of the pointer to just the owner... but then I don't know how iteractive your classes are! :D

So...

CODE
integer clicks;
list positions = [<246,25.844,90>,<246,27,96.250>,<246,30.118,95.867>,<246,27,97.768>,<246,27,96.250>];

moveit()
{
if (clicks + 1 > llGetListLength(positions)) clicks = 0;
llSetPos(llList2Vector(positions,clicks));
}

default
{
state_entry()
{
llListen(2,"","llGetOwner()","reset");
moveit();
}

listen(integer chan, string name, key id, string mess)
{
if (llToLower(mess) == "reset")
{
clicks = 0;
moveit();
}
}

touch_start(integer num_detected)
{
if (llDetectedKey(0) == llGetOwner())
{
clicks ++;
moveit();
}
}

changed(integer change)
{
// new owner then issue new llListen in state_entry
if (change & CHANGED_OWNER) llResetScript();
}

}
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
09-12-2006 15:26
Your code is very readable, which is always good. I like to define globals (variables that run through the whole script) with all caps, and constants, (things that should't change, even though lsl doesn't have constants as a choice) with CONST_<Variable name>. just makes tracking things easier in code when you get a large bit of code, and you have to come back to it 2 months later. Also I like to comment my functions, so I know what they do, again, 2 months down the road. This is just personal prefernce and I'll use your script as an example.


CODE

//********************************************************
//Name: Arrow Slider
//Desc: Slides an object around a prim.
//Authro: Azzura Supplee
//Date: 9/12/2006
//Version: 1.0 (or if your working and just testing stuff) Alpha or Beta
//Modifications: Modified the script to include comments.
//Associated Scripts: <names of scripts that it might interact with or need
//********************************************************

//Incrementer for Position
integer CLICKS;

//List of Positions
list CONST_POSITIONS = [<246,25.844,90>,<246,27,96.250>,<246,30.118,95.867>,<246,27,97.768>,<246,27,96.250>];

//Name: MoveIt()
//Desc: Sets the position of the prim according to the next position indicated in POSITIONS
//Input: None: (you don't have anything that is going into the function)
//Ouput: None (or you can say) The Prim is moved to the next instance in the list
//Globals: integer CLICKS, string POSITIONS
//Usage: MoneIt();
MoveIt()
{

if (CLICKS + 1 > llGetListLength(CONST_POSITIONS)) //reset if you hit the end of the list
//Or you could use
//if(CLICKS >= llGetListLength(CONST_POSITIONS))
{
llSetPos(<246,25.844,90>);
CLICKS = 0;
//(little change of code here, will eliminate the hard coded in position here)
//CLICKS = 0;
//llSetPos(llList2Vector(CONST_POSITIONS, CLICKS));
}

//Ok Couldn't help myself here :)
else
{
llSetPos(llList2Vector(CONST_POSITIONS, CLICKS));
}
}

default
{
state_entry()
{
llListen(2,"","","reset");
}

listen(integer chan, string name, key id, string mess)
{
if (llToLower(mess) == "reset")
{
llSetPos(<246,25.844,90>); //reset to start position
CLICKS = 0;
//Same code as in MoveIt()
//CLICKS = 0;
//llSetPos(llList2Vector(POSITIONS, CLICKS));
//Or you can reset clicks and just call MoveIt() again
//Example:
//CLICKS = 0;
//MoveIt();
}
}

touch_start(integer num_detected)
{
CLICKS++;
MoveIt();
}
}


This is just how I would have done it :) it's all about personal style, and what you use. The main thing is to be consistant in all your code, that way you can read anything you write later :)
Azzura Supplee
Safety Girl
Join date: 16 Jun 2006
Posts: 29
09-12-2006 16:04
Thanks for the comments - maybe I can discipline myself to add more comments!

And thanks pale for the Owner Stuff - that was my next step :)
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-12-2006 22:23
Yea, it's definately important to add comments for 2 reasons. One, if later on you make your code available for other coders, it's nice for them to understand what you were thinking. And two, later on when you decide to revise your code, it's nice for you to remember what you were thinking.

An important issue, and a major pitfall that I find myself doing, is stating the obvious.

For example:
CODE

if (Clicks >= llGetListLength(Positions)) // If Clicks is more than number of elements
{
Clicks = 0; // Reset Clicks to 0
}


Those comments just re-iterate what the lines say. Not very usefull.

CODE

if (Clicks >= llGetListLength(Positions)) // If we have exhasted the list of positions
{
Clicks = 0; // Let's go back to the starting position
}


These comments are probably not the best, but at least give you an idea of what you were thinking and why you did what you did.

This script isn't very complex.. but in mroe complex scripts where you've got code doing all kinds of crazy things, an explanation of why you did something, or what you were thinking is important.

It takes practice, but you'll get the hang of it.

Also, as for the clicks++ and ++clicks part.. the ++clicks does (supposedly) perform faster, but I'm wondering if anyone here knows the difference between clicks++ and ++clicks other than speed? Or in what circumstances you'd use clicks++ over ++clicks?

Edit: Sentance syntax fixing. I tend to type like I think, and sometimes I don't think in complete sentances.
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
09-12-2006 22:30
From: Aakanaar LaSalle

Also, as for the clicks++ and ++clicks part.. the ++clicks does (supposedly) perform faster, but I'm wondering if anyone here knows the difference between clicks++ and ++clicks other than speed? Or in what circumstances you'd use clicks++ over ++clicks?


i do i do

but i hardly ever need the use of clicks++

usually when im incermenting by 1 i want the next number in the sequence, not the old number and an updated varible after the fact
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-12-2006 22:36
From: Osgeld Barmy
i do i do

but i hardly ever need the use of clicks++

usually when im incermenting by 1 i want the next number in the sequence, not the old number and an updated varible after the fact


yea.. it really depends on style.. same as the difference between

if (var <= 3)

vrs

if (var < 4)

It's kinda like slang.. or accents.. different people do it different ways..
But there are times when using var++ is more appropriate.

Btw. Aposiopesisophobia is the fear of not finishing your sen
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
09-12-2006 22:45
From: Aakanaar LaSalle
Y

Also, as for the clicks++ and ++clicks part.. the ++clicks does (supposedly) perform faster, but I'm wondering if anyone here knows the difference between clicks++ and ++clicks other than speed? Or in what circumstances you'd use clicks++ over ++clicks?



The difference between click++ and ++clicks is the order that it performs the incrementation.

clicks++: if used in something like if (clicks++ == 4) means that you are checking clicks if clicks is equal to 4 before you add one to it.

++clicks: if used in if (++clicks == 4) it increments clicks first, then checks.

so in example 1, if clicks is 4 it will check to see if clicks is equal to 4, then increment clicks, so it will go into the if statement, then when it enters the if statement clicks will be 5.

example 2 if clicks is 4, it will add one to clicks, so now clicks is 5, and then check to see if that equals 4, since it doesn't it will skip the if statement.

effectively what the difference is is:

integer clicks = 4;
if(clicks++ == 4) //Check to see if clicks is 4, then add one to clicks
{
}

where the other is
if(++clicks == 4) //Add one to clicks then check to see if it is equal to 4
{
}

Hope I explained that right. Now, I haven't tested this on the LSL compiler, but I assume it's the same as C++. If i'm wrong, please state so :)
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-12-2006 22:54
You're right.. it's somethign I use on C and C++ as well.. I just got the feeling from a lot of the example code i've seen here, that a lot of ppl don't know the difference.. and to make it more clear for those who are wondering.

Var++ and ++Var both increment the Var by 1, and they both also return the value of Var. eg:
int = num++
is the same as
int = num

The difference is, whether it increments before returning the value of Var or after.
++Var increments Var by 1 first, then returns the new value of Var
Var++ returns the old value of Var first, then increments it.

I know some of you know the difference, but from what i've seen, some others could use this little refresher
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
09-13-2006 04:59
From: Aakanaar LaSalle
You're right.. it's somethign I use on C and C++ as well.. I just got the feeling from a lot of the example code i've seen here, that a lot of ppl don't know the difference.. and to make it more clear for those who are wondering.

Var++ and ++Var both increment the Var by 1, and they both also return the value of Var. eg:
int = num++
is the same as
int = num

The difference is, whether it increments before returning the value of Var or after.
++Var increments Var by 1 first, then returns the new value of Var
Var++ returns the old value of Var first, then increments it.

I know some of you know the difference, but from what i've seen, some others could use this little refresher


I think the other reason that its 'faster' , in C/C++ at least, is to do with the creation of a temporary variable to store the result of the increment for transfer back to the original post test.