Scripting Language Operators

Post your scripting questions / solutions here

Moderator: Moderators

Post Reply
Killerdude
Sergeant Major
Posts: 110
Joined: Fri May 11, 2007 12:16 am

Scripting Language Operators

Post 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
User avatar
bdbodger
Moderator
Posts: 2596
Joined: Tue Feb 25, 2003 7:34 am
Location: canada
Contact:

Post 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 .
Image
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post 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))
Image
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post 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.
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
User avatar
ViPER
General
Posts: 1058
Joined: Fri Jan 14, 2005 5:48 pm
Location: California
Contact:

Post by ViPER »

for the scribes :D Please

what does ^ do ?
Killerdude
Sergeant Major
Posts: 110
Joined: Fri May 11, 2007 12:16 am

Post 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:
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post 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. :)
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

^= won't work, ! will do exactly what you want :)
Image
Killerdude
Sergeant Major
Posts: 110
Joined: Fri May 11, 2007 12:16 am

Post 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.
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post by Rookie One.pl »

Yep, that's one thing I used to miss, too.
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
Post Reply