Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

ifs and elses

Danielz Shackle
Registered User
Join date: 30 Apr 2006
Posts: 100
05-12-2006 02:12
this is an easy question, but i cant seem to get it working right. Ive scripted before, but nwe to sl so next few days ill have a few small questions on sl grammer

im trying to loop a state, end of state looks something like this

{
if (a > 0)
{
state jim; (this is where im trying to have it loop the current state)
}
else
{
state bob; (goes to next state)
}
}

is there a defferent way to do it or am i missing something? I have object talking throughout script for testing purposes and I know it gets to this, but never loops.
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
05-12-2006 02:17
What do you mean, end of state? state_exit()? You don't want to be putting state change commands into a state_exit(), that fires after you've changed the state from some other part of the script....
Danielz Shackle
Registered User
Join date: 30 Apr 2006
Posts: 100
05-12-2006 02:20
what i mean is when everything is done in the state im in, i want it to either loop and redo commands in state, or go on to next state. i dont have a state_exit in script, is that the problem?
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
05-12-2006 02:26
No, you don't need a state_exit(), that won't be it.

To be perfectly honest I don't think I've ever tried calling a state from within that state - I suspect it won't work, or cause problems. That might be it. You're not really "looping" anything if you do that anyway, all that would happen if it works would be that you call state_entry() again.

If you want to do that, why not put the code that you call in state_entry() into a function and call that in both instances? e.g.

CODE

startup()
{
// some code here that you call at the start of the state
}

state jim
{
state_entry()
{
startup();
}

touch_start(integer n)
{
if (a > 0) startup();
else state bob;
}
}
Danielz Shackle
Registered User
Join date: 30 Apr 2006
Posts: 100
05-12-2006 02:45
im not sure if its the code or me, but i tried what you recommended and it i cant even get rid of script errors to try it.
Danielz Shackle
Registered User
Join date: 30 Apr 2006
Posts: 100
05-12-2006 02:48
maybe its the touch start, i took that out because i dont want to hace to touch it in middle of script. does it need something to start it again after

state jim
{
state_entry()
{
startup();
}

or can i have it go straight to my other stuff i want it to do
Starax Statosky
Unregistered User
Join date: 23 Dec 2003
Posts: 1,099
05-12-2006 02:51
or try something like:

CODE

state jim
{
state_entry()
{
while (a>0)
{
//Do stuff
}
state bob;
}
}
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
05-12-2006 02:57
touch_start() was just an example. You need to have something that sets it off within that state, a point where it says "okay, if a <= 0 move to this new state, otherwise restart this one".

It's a bit tricky to be more precise without seeing the script itself, I'm not really sure what you're trying to do.
Blueman Steele
Registered User
Join date: 28 Dec 2004
Posts: 1,038
05-12-2006 03:03
Hi Danielz,

It sounds like you wish a state to "restart" a set of instructions? I may need to have a better idea of what you are attempting in order to help though, but here are a few common snags.

States do not run their code in a linear fashion and this can be confusing (was to me) to new LSL programers.

An LSL program looks like this.

STATE 1
----event A
(code for event)

----event B
(code for event)

----event C
(code for event)

STATE 2
----event A
(code for event)

----event B
(code for event)

----event C
(code for event)

Where states are groups of events that can get triggered in any order and even simultaniously. When you switch states you are turning off every event in that state and switching to a new one. All data is lost unless you hold it in globals above the level of the states themselves.

Where I wrote (code for event) is where you would tell a state to restart or go to a new state.

Please take a look at my tutorial here...

http://www.instructables.com/ex/i/0BFD9980243F1029BC6B001143E7E506/

It will outline a simple two state model starting from basic principles.

A state will run until it a new one is called so in effect it's its own loop and will always run the state events it contains. Loops are normally for code within events. Can you tell us a little more about what effect you are going for?

- Blueman Steele

From: Danielz Shackle
this is an easy question, but i cant seem to get it working right. Ive scripted before, but nwe to sl so next few days ill have a few small questions on sl grammer

im trying to loop a state, end of state looks something like this

{
if (a > 0)
{
state jim; (this is where im trying to have it loop the current state)
}
else
{
state bob; (goes to next state)
}
}

is there a defferent way to do it or am i missing something? I have object talking throughout script for testing purposes and I know it gets to this, but never loops.
Danielz Shackle
Registered User
Join date: 30 Apr 2006
Posts: 100
almost there
05-12-2006 03:09
Starax Statosky
state jim
{
state_entry()
{
while (a>0)
{
//Do stuff
}
state bob;
}
}
worked perfect in that it reran through script till it stoped itself on correct integar, only question is , after thile is done, what command sends it to next state or command?
______
and bluman, im going to read ur ptutorial rightnow
Danielz Shackle
Registered User
Join date: 30 Apr 2006
Posts: 100
05-12-2006 03:14
Blueman Steele, that page will help some new people, it breaks thing sdown pretty well.

I found my last problem ,

Starax Statosky script worked perfect, i just had to add a } way at end somewhere ...

thatnks for the ideas everyone
Starax Statosky
Unregistered User
Join date: 23 Dec 2003
Posts: 1,099
05-12-2006 03:17
From: Danielz Shackle
Starax Statosky
state jim
{
state_entry()
{
while (a>0)
{
//Do stuff
}
state bob;
}
}
worked perfect in that it reran through script till it stoped itself on correct integar, only question is , after thile is done, what command sends it to next state or command?
______
and bluman, im going to read ur ptutorial rightnow



'state bob' will send it off to bob. Assuming that bob is present.

I think the mistake you was making is that you was trying to treat the 'state' command like the old 'goto' command in basic. When looping in LSL, we typically use timers, the for() function, or the while() function.