Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

While loop question

Adam Ramona
Registered User
Join date: 5 Jan 2005
Posts: 56
12-27-2006 20:54
Hello all,
Can anyone tell me why the below script can't get out of the while loop?
CODE

//this script makes an object rotate around an offest point, with touch toggling it on and off. Off sets the object back to its starting position and rotation.
offsetRot(vector rotPoint, rotation rotAngle)
{
rotation new_rot = llGetRot() * rotAngle;
llSetRot(new_rot);

vector currentPos = llGetPos();
vector newPos = rotPoint + ((currentPos - rotPoint) * rotAngle);
llSetPos(newPos);
}
vector startPos;
rotation startRot;
integer moving = FALSE;
default
{
state_entry()
{
startPos = llGetPos();
startRot = llGetRot();
}
touch_start(integer BLAH)
{
if(moving == FALSE)
{
moving = TRUE;
//when the while loop is commented out, the whole thing works exactly as the logic would suggest. But when the while loop is active, it never makes it to the "else" below. I tried separating it out into states, but exactly the same problem.
// while (moving == TRUE)
// {
rotation myAngle = llEuler2Rot( < 0, 0, 3 * DEG_TO_RAD > );
vector myRotPoint = startPos + <0.5,0.5,0.0>;
offsetRot(myRotPoint, myAngle);
llSay(0,"going");
// }

} else if(moving == TRUE)
{
moving = FALSE;
llSetPos(startPos);
llSetRot(startRot);
llSay(0, "stopped");
}
}
}


Any help most appreciated.
Trevor Langdon
Second Life Resident
Join date: 20 Oct 2004
Posts: 149
12-27-2006 21:49
Adam--
Once you get into your While loop, you are stuck repeating the loop forever. You need to add a condition where 'moving' is set to FALSE within your While loop. As it is coded now, it becomes an infinite loop when 'moving' is TRUE.
Peekay Semyorka
Registered User
Join date: 18 Nov 2006
Posts: 337
12-28-2006 00:55
Hi Adam,

Since LSL is "event-driven", it's not a good idea to use a loop in that manner because no other events can trigger "while" the loop is running (pardon the pun.) Alternatively one could rotate the object in step with a timer. See this thread for an example.

However, to smoothly rotate an object consider using llTargetOmega() instead. To rotate the object around a point, link the object to (a possibly invisible) root prim, then call llTargetOmega from the root prim. Your object will then smoothly "orbit" around its root prim. (Note: your object must be the child prim.)

Then the "touch" handler can simply set/reset the llTargetOmega, and make the linked root prim visible/invisible if desired.

-peekay
Adam Ramona
Registered User
Join date: 5 Jan 2005
Posts: 56
Thanks
12-28-2006 14:33
Thanks for the clear explanations and suggestions, most appreciated and helpful.

Adam