Page 1 of 1

Scripting Language Operators

Posted: Wed Feb 20, 2008 2:13 am
by Killerdude
If I remember correctly a boolean exclusive ored with 1 will toggle the boolean. Is there a syntax for that in the Quake Scripting Language? ... || maybe?

Also, will a boolean evaluate out in a conditional operator?

ie)

Code: Select all

local.IsFanOn = 1

if (IsFanOn){    // <----- will this evaluate as true??
       Do Something}
Thanks in advance
KD

Posted: Wed Feb 20, 2008 9:12 am
by bdbodger
0 = false 1= true thats why you see a lot of threads that have while(1) in them to create a continuous loop .

if (local.IsFanOn) will work variables have to be game local or level variables .

Posted: Wed Feb 20, 2008 5:27 pm
by jv_map
You can use ^ for bitwise xor. Or a combination of && and ||:

if(local.a ^ local.b)

if((local.a || local.b) && !(local.a && local.b))

Posted: Wed Feb 20, 2008 7:46 pm
by Rookie One.pl
You basically get the same operators you have in C. Only that the bitwise ones sometimes do not work as intended.

Posted: Wed Feb 20, 2008 8:48 pm
by ViPER
for the scribes :D Please

what does ^ do ?

Posted: Wed Feb 20, 2008 10:57 pm
by Killerdude
Viper wrote:for the scribes :D Please

what does ^ do ?
Thats the answer I was looking for. Thanks JV
^ is the bitwise operator for Xor (Exclusive Or). The truth table for Xor is as follows:
  • 0 xor 0 = 0
    0 xor 1 = 1
    1 xor 0 = 1
    1 xor 1 = 0
So what this tells you is that you only get a true (1) when one of the booleans is true and not both. A practical application of this is to toggle a boolean.

ie)

Code: Select all

local.isfanon = 0 // Fan is off
local.isfanon^1 // Fan has been toggled on
local.isfanon^1 // Fan has been toggled off
The first xor replaces typing:

Code: Select all

if (local.isfanon){
       local.isfanon = 0}
else{local.isfanon = 1}
The plus side is its less to type, the minus side is its not clear to someone that is unfarmillar with the xor operator. Then again thats what comments are for.

Anyone please correct me if I am wrong :roll:

Posted: Thu Feb 21, 2008 7:34 pm
by Rookie One.pl
Killerdude wrote:Anyone please correct me if I am wrong :roll:
I hope you don't mind if I do. ;)

Code: Select all

local.isfanon = 0 // Fan is off
local.isfanon^1 // Fan has been toggled on
local.isfanon^1 // Fan has been toggled off
This code won't work as it makes statements without effect. Those last 2 lines are just values that are not assigned to anything and will probably break your script (I think the interpreter expects every statement to either perform a value assignment or an event call). What you probably meant is:

Code: Select all

local.isfanon ^= 1
...which performs an assignment of the current local.isfanon value xored with 1 to local.isfanon. At least it would do so in C. ;) It is equal to writing:

Code: Select all

local.isfanon = local.isfanon ^ 1
However, the MoHAA script interpreter is somewhat messy and I'm not sure this language construct is supported by it (although *=, += and -= - the multiplication, addition and subtraction assignments do work). So in case it does not work, I would rather suggest using:

Code: Select all

local.isfanon = !local.isfanon
Which will do exactly the thing you want - ! is the boolean negation operator. :)

Posted: Thu Feb 21, 2008 7:40 pm
by jv_map
^= won't work, ! will do exactly what you want :)

Posted: Thu Feb 21, 2008 7:50 pm
by Killerdude
Thanks for the correction. It has been a while since I have done any c/c++. Adding scripting to my map is helping me to remember how much fun it is !!! I just wish there was a debugger like a c compiler has.

Posted: Thu Feb 21, 2008 10:55 pm
by Rookie One.pl
Yep, that's one thing I used to miss, too.