Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

OR's and AND's confusion

Designer Madonna
Designer
Join date: 14 Oct 2005
Posts: 9
05-18-2006 01:43
I am looking a three agent status conditons. If any one of them is true then take an action. If none are true stop the action. I thought that using || between each possiblity would work:

integer avState = llGetAgentInfo(llGetOwner());

if (avState == x || avState == y || avState == z)
{
do smethting
}
if (avState != x && avState != y && avState != z)
{
stop doing it
}

It appears that the3rd avState is not evaluated, but I am missing how to make that happen.
Russell Hansen
Texi pets are here!
Join date: 11 Apr 2006
Posts: 107
05-18-2006 02:33
You are using boolean operators, which only test the TRUE or FALSE values of a variable. You need to use the bitwise operators "&" and "|" instead.
Jigsaw Partridge
A man of parts
Join date: 3 Apr 2005
Posts: 69
05-18-2006 02:41
Do you need the second 'if'? Wouldn't just an 'else' after the first iff do what you need it to do (i.e. stop doing it if avState isn't x,y or z)?

That having been said, the code should still work as you have posted it, assuming LSL boolean operator precedence is the same as that for C, with '!=' being higher than '&&'. Try putting brackets round the sub-expressions to force it to calculate the '!=' parts first, before te '&&'s.
Jigsaw Partridge
A man of parts
Join date: 3 Apr 2005
Posts: 69
05-18-2006 02:46
Yes, Russell is right if you are testing the individual bits set in avState, I was just looking at it as a logical test. It depends what you are using for x,y and z, lol.
Designer Madonna
Designer
Join date: 14 Oct 2005
Posts: 9
Follow up
05-18-2006 02:50
That appear to work as far as recognizing the 3rd status, but it is still not triggering the action.
What is (llGetAgentInfo(llGetOwner()) == 2118); and is there somehting special about the way it is handled?
Bitzer Balderdash
Dazed and Confused
Join date: 21 Dec 2005
Posts: 246
05-18-2006 03:03
From: Designer Madonna
That appear to work as far as recognizing the 3rd status, but it is still not triggering the action.
What is (llGetAgentInfo(llGetOwner()) == 2118); and is there somehting special about the way it is handled?


well 2118 decimal is 0x846 in hex, so, checking the wiki for the flags says that if the agent IS
AGENT_BUSY Returned if agent is in "busy" mode. 0x0800
AGENT_AWAY Returned if agent is in "away" mode. 0x0040
AGENT_SCRIPTED Returned if agent has scripted attachments. 0x0004
AGENT_ATTACHMENTS Returned if agent has attachments. 0x0002

i.e. the are busy, and away, and have scripted attachments, but simultaneously they are NOT any of the following
AGENT_ALWAYS_RUN Returned if agent has running ("Always Run";) enabled. 0x1000
AGENT_CROUCHING Returned if agent is crouching. 0x0400
AGENT_FLYING Returned if agent is flying. 0x0001
AGENT_IN_AIR Returned if agent is in the air (hovering). 0x0100
AGENT_MOUSELOOK Returned if agent is in mouselook. 0x0008
AGENT_ON_OBJECT Returned if agent is sitting on an object. 0x0020
AGENT_SITTING Returned if agent is sitting. 0x0010
AGENT_TYPING Returned if agent is typing. 0x0200
AGENT_WALKING Returned if agent is walking. 0x0080



What exactly are you trying to do here? And where did you get the magic 2118 number from if you're not sure what it is meant to do?
Paul Churchill
Pie are squared
Join date: 8 Sep 2005
Posts: 53
05-18-2006 04:40
Designer, this should get you on the right track


CODE
 key id;
default
{
state_entry()
{
id = llGetOwner();
llSetTimerEvent(1);

}

timer() {

integer avState=llGetAgentInfo(id);

if (avState &(AGENT_FLYING | AGENT_WALKING | AGENT_AWAY | AGENT_BUSY) ) {
llSetText("Ignore me", <1,1,1>, 1);
}
else {
llSetText("Speak to me", <1,1,1>, 1);
}
}
}

the crux here is
CODE
if (avState &(AGENT_FLYING | AGENT_WALKING | AGENT_AWAY | AGENT_BUSY) )


this essentially says "if any of the bits in the avState flag match one or more of the bits in the flying, walking, away or busy flags then do this....

By the way, this is equivalent to avState & 2241

HTH

Paul.
_____________________
If there are two ways to interpret something I've said and one of them offends or upsets you, I meant the other one.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
05-18-2006 07:29
you can write 2118 as 0x846 in your code. btw, you will get a bit of a speed savings by using 0x846 vs (AGENT_FLYING | AGENT_WALKING | AGENT_AWAY | AGENT_BUSY). The LSL compiler does not do any optimizations. Unless you're scraping the bottom of the barrel for cpu savings, i won't bother.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Paul Churchill
Pie are squared
Join date: 8 Sep 2005
Posts: 53
05-18-2006 07:37
From: Strife Onizuka
you can write 2118 as 0x846 in your code.

......

Unless you're scraping the bottom of the barrel for cpu savings, i won't bother.


I'd agree with this - I can be extremely good at write-only code.

I'll develop a bit of code - and when I look at it a week or a month later.... "what did I intend this to do?"

:)

Paul.


Edit : corrected typo
_____________________
If there are two ways to interpret something I've said and one of them offends or upsets you, I meant the other one.