Jump to content

Difference between "!" and "FALSE"


testgenord1
 Share

You are about to reply to a thread that has been inactive for 1263 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi!
I've written a script that starts on touch_start.
After starting the script, the touch_start event is supposed to be deactivated for some time,
so that nobody can restart the script as long as the process has not been finished:

integer on;
touch_start (integer num)
    {
        if(!on)
        {
            on = TRUE;

            *do something*

        }
        else
        {
            return;
        }

This seems to work
(the only thing I'm not quite sure about is the "else" part with "return". Couldn't you just leave the "else" part out?)

Now my main question:
I've tried a similar version, which does NOT work,
and I don't really understand the difference:

integer on;
default
{
state_entry()
{
	on = FALSE;
	touch_start (integer num)
    	{
        	if(on = FALSE)
        	{
            		on = TRUE;

            		*do something*
        	}
        	else
        	{
            		return;
        	}
	}
}
}

The problem apparently is that "!on" and "on = FALSE" don't seem to be the same thing.
Could one of you explain the difference between "!on" and "on = FALSE"  to me?

(Besides: Is it necessary to declare the integer "on" as FALSE at the beginning?
Is integer "on" not FALSE by default before it is turned TRUE?)

Thank you very much in advance!

Edited by testgenord1
Link to comment
Share on other sites

5 minutes ago, testgenord1 said:

(Besides: Is it necessary to declare the integer "on" as FALSE at the beginning?
Is integer "on" not FALSE by default before it is turned TRUE?)

Right.  An integer variable's value is 0 by default.  You're using on as a Boolean, so that's interpreted as FALSE.  You don't have to set it when you define it.

  • Like 1
Link to comment
Share on other sites

if (on = FALSE) assigns FALSE to on. Which means that on is always FALSE. Should be if (on == FALSE) meaning is on TRUE or FALSE

LSL takes from the C language.  = means assign. == means compare. Unlike in BASIC where = is used for both assign and compare. The BASIC compiler knowing the difference from the context

  • Like 3
Link to comment
Share on other sites

1 minute ago, testgenord1 said:

(the only thing I'm not quite sure about is the "else" part with "return". Couldn't you just leave the "else" part out?)

Yes, it can simply be left out.

Your second bit of code has two big problems.

  • You can't put an event inside of another event. Move touch_start out of state_entry.
  • if (on = FALSE) is an assignment, not a comparison. You are always assigning a false value to "on" and the test will never pass.
6 minutes ago, testgenord1 said:

(Besides: Is it necessary to declare the integer "on" as FALSE at the beginning? Is integer "on" not FALSE by default before it is turned TRUE?)

All variables are initially set to zero, or an empty string/list. It's not necessary to give them that value, but it helps with clarity.

7 minutes ago, testgenord1 said:

The problem apparently is that "!on" and "on = FALSE" don't seem to be the same thing.
Could one of explain the difference between "!on" and "on = FALSE"  to me?

"on = FALSE" is an assignment. It gives a value to "on".

"!on" is an expression. It does something that results into some value.

"!" is the logical "not" operator. It switches any nonzero ("true") value to zero ("false"), and vice versa. The value is not assigned to anything, so the value of "on" does not change.

  • Like 2
Link to comment
Share on other sites

@testgenord1

you can also do an assignment inside your if statement...kinda like...

integer on;
touch_start (integer num)
{
  if( on = !on) // toggle the variable and check if it is true or not
  { llOwnerSay("ON");
  }
  else
  { llOwnerSay("OFF");
  }
}

 

Edited by Xiija
Link to comment
Share on other sites

To be clear, think of Xiija's example in two steps:

1.  Reverse the binary value of the variable on, so that it becomes its opposite.  If it is currently 0 (FALSE), make it  1 (TRUE).  If it's currently 1, make it 0.

2. Then, ask whether the result of that change is TRUE or FALSE.  So, if the operation on = !on made the value of on equal to  1,  if (on = !on) is TRUE, and vice versa.

You can generalize that approach by putting any assignment you wish into an if test.  So all of these will work:

      if ( ( fStemAngle =  vSun.z  + 0.05)   > 0.0 )  //  Make a plant stem bend by a small amount if the sun is above the horizon by 0.05

      if ( (strCurrentAnim = llList2String(lAllMyAnims, ++i%5) ) == "Happy Dance")   // Start the music if the new anim is your Happy Dance anim

      if (  (integer)(vColor.x =  1.0*(on = !on) ) )   // Ring a warning bell or something if toggling on has made the R component of RGB = 1.0

In each case, you are changing the value of some variable -- usually so you can use the changed value in some other setting later -- and are then testing the new value to see whether some other related change is warranted.  Much of the time, you can write things like this in much less confusing terms by not putting the assignment into the if test, but this approach can sometimes give you a tighter code.  Sometimes it can result in code that it really hard to read, too, so do it with caution.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1263 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...