Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

My if test is failing. Why?

Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
11-23-2008 09:25
This is such a newbie problem I feel embarrassed. I've programmed for AGES, but LSL is still new to me. I've been playing with what should be a VERY simple script and I have a conditional test that is non-responsive. I'll copy it below.

I have peppered the script with llOwnerSay statements to track the flag called "seen," should flip from 1 to -1 every time state reset jumps back to state default. It does fine until the second time I jump to state reset. As the script executes state_entry at that point, seen=-1 as it should. It OUGHT to fail the first if test and go straight to the "else if" condition, but it doesn't. I can't see why not, for the life of me....

OK, here's the script. Please don't laugh. I know it's dirty. I'm not trying to write an efficient script --- just one I can follow while I figure out what I'm doing.

CODE

float period = 10.0; // Set this value to determine how long before the object changes state
integer seen = 1;
default
{
on_rez(integer start_param)
{
llResetScript();
seen = 1;
}

state_entry()
{
llOwnerSay ("Now seen = " + (string)seen);
if (seen != 0)
{
float x;
if (seen == 1)
{
llSetAlpha(1.0,ALL_SIDES);
llSleep(period);
state restart;
}
else if (seen == -1)
{
llSetAlpha(0.0,ALL_SIDES);
llSleep(period);
state restart;
}
}
else
{
llSetAlpha(1.0,ALL_SIDES);
state restart;
}
}

touch_start(integer total_number)
{
if (llDetectedKey(0)==llGetOwner())
{
seen = 0;
llSetAlpha(1.0,ALL_SIDES);
llOwnerSay("Hey Abbey! The script has stopped. Touch me again to restart it, please.");
state restart;
}
}
}

state restart
{
state_entry()
{
llOwnerSay ("And now seen = " + (string)seen);
if (seen = 1) // << Here's where the script doesn't branch properly if seen = -1 WHY?????!!!!
{
float x = 0;
float step;
for (x; x <= 10.0; x++)
{
step = (10.0 - x) / 10.0;
llSetAlpha(step,ALL_SIDES);
llSleep(period/10);
}
seen = -seen;
llOwnerSay ("seen = " + (string)seen);
state default;
}
else if (seen = -1)
{
llOwnerSay ("OK, NOW seen = " + (string)seen);
float x = 10.0;
float step;
for (x; x >= 0; x--)
{
step = (10.0 - x) / 10.0;
llSetAlpha(step,ALL_SIDES);
llSleep(period/10);
}
seen = -seen;
llOwnerSay ("And FINALLY seen = " + (string)seen);
state default;
}
else if (seen = 0)
{
state default;
}
}


touch_start(integer total_number)
{
if (llDetectedKey(0)==llGetOwner())
{
seen = 1;
llSetAlpha(1.0,ALL_SIDES);
llOwnerSay("The script has restarted. Thank you, Abbey!");
state default;
}
}

}
Anti Antonelli
Deranged Toymaker
Join date: 25 Apr 2006
Posts: 1,091
11-23-2008 09:35
Add another = sign to the problem line.

Don't feel bad, we've all done it.

---

edit: if you get in the habit of typing tests backwards, with the constant first, like this:

if (1 == seen)

then if you ever miss the second equals sign the compiler spits out an error at that line so you know to go fix it.

---

edit again: all the tests in state restart have the same problem. Still not laughing, I've been there more often than I care to admit publicly :o

---
_____________________
Designer of sensual, tasteful couple's animations - for residents who take their leisure time seriously. ;)

http://slurl.com/secondlife/Brownlee/203/110/109/

Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
11-23-2008 09:38
Yes that is bad;)
never write: "if (seen = 1)" when you mean: "if (seen == 1)"
(first one will set value of 'seen' to one and will be TRUE anytime)
_____________________
From Studio Dora
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-23-2008 09:52
And try to get in the habit of putting the variable on the right side. If you do that then it will throw a compiler error if you miss one of the ='s.

Before you correct your script try changing it like this:

if (1 = seen)

and try compiling again.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
11-23-2008 10:11
/me really is embarrassed. I was so focussed on other possibilities that it never occurred to me that I had written that as a replacement instead of a condition.:o Thank you SO much.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
11-23-2008 11:53
it's an easy mistake to make, type to fast, get to thinking semanticly, etc ad nauseam...

show me a programmer that says they never make that mistake and I'll show you a big fat liar =)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
11-25-2008 01:03
Also, in your on_rez, you reset the script, so the second line (seen = 1) never gets executed. you already set the variable globally, so when it resets, it still works, but you have an extra line of code there that never gets executed.
_____________________
My tutes
http://www.youtube.com/johanlaurasia