Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

a lil' help with brackets!

Fudgey Jenkins
Registered User
Join date: 9 Sep 2007
Posts: 81
05-05-2008 05:04
been working on learning scripting, it seems to be the only thing I cant do in SL... :(

my major problem seems to be the placing of brackets.. like so.

default
{
state_entry()
{
llSay(0, "Hello, Avatar!";);
}

touch_start(integer total_number)
{
llSetTexture( "Handle", ALL_SIDES )
{
state new;

}
{

state new {
state_entry() {
llSay(0, "Hello, Avatar!";);
}
}

___________________________________

just playing around really.. I'm having trouble in alot of things, I got a cube to change texture when clicked, but if I want to add a new state, I have no idea how to add it at the end of the first event.. guess I need a bit more training.. any sugjestions for places to learn or web tutorials? :) exuse my grammar and all that it's late! :P
Pale Spectre
Registered User
Join date: 2 Sep 2005
Posts: 586
05-05-2008 06:05
Rough guide:

· lines inside braces should be terminated with: ;
· braces should always be paired.
· states are defined separately... no states within states allowed.


// any global variables would go here, and only here

default
{
state_entry()
{
llSay(0, "Hello, Avatar! ...from default";);
}

touch_start(integer total_number)
{
llSetTexture( "Handle", ALL_SIDES ); // terminate with ;
//{
state new;
}
//{
} // close default state

state new
{
state_entry()
{
llSay(0, "Hello, Avatar! ...from new";);
}
}
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
05-05-2008 09:31
Curly braces ({}) are used to build a block of code--a list of statements--where normally you would only have one. In conditional (if, else, else-if) and loop (for, while, do-while) statements you can either have a single statement or a block of code delimited by curly braces. Most programming style guides recommend with good reason that you ALWAYS use the braces, even when you DO only have one statement to execute. For example (please hit the forum "Quote" button to see the proper indentation):

CODE

if (myVariable < yourVariable)
nahNah(); // You CAN do this, but you generally shouldn't

for (i = 0; i < someBigNumber; ++i)
llOwnerSay("Wheedle while you work"); // Another ouchy one (not to mention self-SPAM! ;-)

if (myVariable < yourVariable)
{
nahNah(); // Much better
}

for (i = 0; i < someBigNumber; ++i)
{
llOwnerSay("Wheedle while you work"); // Now we're getting there (though still self-SPAM)
}

if (message == "execute a few statements")
{
doSomething(); // Without the braces, this would be the only statement in the 'if'
++incrementThis; // Without the braces, this would run always, not just when the condition is true
doSomethingElse();
}

if (message == "execute a few statements")
doSomething();
++incrementThis; // See? Probably not what we had in mind. Indent any way you want, but these
doSomethingElse(); // are their own statements, not part of the conditional

if (message == "oops")
doSomething;
++incrementThis; // Definitely not what we wanted in this case; indentation fixed to show actual structure
else // Error, because else must follow an if, not a standalone statement
doSomethingElse();


For functions and event handlers their purpose is similar but they happen to be mandatory (even if there is only one statement in the function/event handler, or none at all). For states the curly braces are also required and serve to group the event handlers that belong to that state. For example:

CODE

myFunction()
{ // Always need the braces here
foo();
}

default
{
state_entry()
{ // Also here
llOwnerSay("Hello World!");
state myOtherState;
}

timer()
{} // Need braces here, even though there aren't any statements!
}

state myOtherState
{
state_entry()
{
}
}
CODE
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
05-05-2008 10:51
Properly written, your code should look like this...
CODE

default
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}

touch_start(integer total_number)
{
llSetTexture( "Handle", ALL_SIDES )
state new;
}


state new
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}
}


There's a couple of different coding styles out there, personally, I like to line up my brackets so that the code is much easier to read as well as debug. If you look at the code above (quote this message if using IE to see the layout), the state brackets, and each event brackets line up vertically, therefore, if you miss a bracket somewhere, it's pretty easy to follow the brackets up or down vertically and see where you're missing a bracket. Often times, you'll see this style...
CODE

default {
state_entry() {
llSay(0, "Hello, Avatar!");
}

touch_start(integer total_number) {
llSetTexture( "Handle", ALL_SIDES )
state new;
}


state new {
state_entry()
{
llSay(0, "Hello, Avatar!");
}
}


which just makes my eyes swim. Not so much in this simple example, but in more complex code, I generally will take to time to go 'fix' the bracketing so I can more easily read the code. Basically, it's a matter of personal preference, so one way over the other is not the 'correct' way, so it's just my preference to code with brackets that line up vertically.

The issue in your code as you wrote it was that you dont need the { between the llSetTexture() and state new statement. Here's a more generic look at how things should be written...
CODE

// global variable declarations here
type variable = 'data';

// function declarations here

type function (variable 1, variable 2, etc)
{
function code
}


state1 // in LSL the first state entered is always the default!
{
event1 ()
{
event 1 code;
more code;
stil more code;
}

event2 ()
{
event 2 code;
}
} // end of state1

state2
{
event1()
{
event1 code;
}

event2()
{
event2 code;
}
}


Flow control statements, such as for, if-else, while, and do-while are statements, not functions, therefore do not get a semi-colon at the end of the line...
CODE

integer i;
for (i=0; i < someValue; i++) //<-- note no semi colon
{
my repeating code here...
}

while (condition)
{
statements;
}

do
{
llSay(0, "monkey!");
}
while (monkeysRemain());

etc...


Jumps and returns DO get a semi colon, personally, I never ever ever ever ever use jump. It's just bad programming practice and if you use it, shoot yourself :)
Saladin Nightfire
Scripter for HIRE
Join date: 6 Jan 2008
Posts: 20
05-05-2008 15:27
CODE


default //Is a state
{ // opening bractet to make it know what is included in it
state_entry() // Event to do somthing when it says.. IE. touch,timers, and others
{ // opening bracket for event so it know what is included in it....
llSay(0,"hiya"); // Function that happens in the event.. each funtion ends with ; to close it out so script knows that it has ended ....
} // Ends bracket for event
} //ending bracket for state.. now the script knows what is included in the states and events and what functions have ended..



tried to make it simple and to the point no more extra junk right to the point and show u in a script what needs to be done:)
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
05-05-2008 19:21
From: Saladin Nightfire
CODE


default //Is a state
{ // opening bractet to make it know what is included in it
state_entry() // Event to do somthing when it says.. IE. touch,timers, and others
{ // opening bracket for event so it know what is included in it....
llSay(0,"hiya"); // Function that happens in the event.. each funtion ends with ; to close it out so script knows that it has ended ....
} // Ends bracket for event
} //ending bracket for state.. now the script knows what is included in the states and events and what functions have ended..

CODE
  

tried to make it simple and to the point no more extra junk right to the point and show u in a script what needs to be done:)


Try using [ PHP ] (w/o spaces) and [ /PHP ] to close.
Fudgey Jenkins
Registered User
Join date: 9 Sep 2007
Posts: 81
05-18-2008 03:09
those codes dont compile :(

I'm getting pretty good with one state scripts, and I thank you for the help you gave me ;)

anyway, I can get everything compiled but I cant make a new state, it says syntax error at

state new;

I copied some codes from this post, and I got the same stop :(

does state new need some details with it?

*looks at more guides*

EDIT!!

default
{
touch_start(integer a)
{
llOwnerSay("ffff";);
state new;
}
}

state new
{
touch_start(integer a){
llOwnerSay("ggg";);
state default;
}
}

I wrote that one and it worked =D
Keira Wells
Blender Sculptor
Join date: 16 Mar 2008
Posts: 2,371
05-18-2008 03:33
From: Fudgey Jenkins
those codes dont compile :(

I'm getting pretty good with one state scripts, and I thank you for the help you gave me ;)

anyway, I can get everything compiled but I cant make a new state, it says syntax error at

state new;

I copied some codes from this post, and I got the same stop :(

does state new need some details with it?

*looks at more guides*

EDIT!!

default
{
touch_start(integer a)
{
llOwnerSay("ffff";);
state new;
}
}

state new
{
touch_start(integer a){
llOwnerSay("ggg";);
state default;
}
}

I wrote that one and it worked =D

Glad you got one working ^_^

For me, I've found a few times that instead of a ; I type a : by accident, and it's a little hard t notice. Perhaps on your first attempt you did that, or accidentally forgot the return.

Either way, state changes need no further parameters, just
state newstatename;
.

Have fun!
_____________________
Tutorials for Sculpties using Blender!
Http://www.youtube.com/user/BlenderSL