HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Or's and And's

02-09-2008, 02:00 AM#1
Tastingo
Hey I need some help with Or's and And's. Not really sure how you use them in JASS. If you post an example of how to use them, it would be greatly appreciated. I know about GetBooleanAnd and GetBooleanOr, but I'm not really sure how they work and if there is a better way to do it.
02-09-2008, 02:27 AM#2
Earth-Fury
Collapse JASS:
function GetBooleanAnd takes boolean valueA, boolean valueB returns boolean
    return valueA and valueB
endfunction

"and" and "or" are boolean operators. They have operands on the left and right.

"and" compares two boolean values, and if both are true, it results in true. thus:
Collapse JASS:
true and true // results in true
false and true // results in false

"or" compares two boolean values, and if either is true, it results in true. thus:
Collapse JASS:
true or false // results in true
false or true // results in true
false or false // results in false
true or true // results in true

GetBooleanAnd() and GetBooleanOr() are examples of completely useless functions.

"and" and "or" are executed in a short circut manner, meaning that if an answer is determinate from the current execution, execution stops. so:
Collapse JASS:
ReturnsFalse() and ReturnsTrue() // ReturnsTrue() is never called
ReturnsTrue() or ReturnsFalse() // ReturnsFalse() is never called
02-09-2008, 02:33 AM#3
Tastingo
ah ok thank you, but does it always have to call another function:
Collapse JASS:
ReturnsFalse() and ReturnsTrue() // ReturnsTrue() is never called
You have it so its calling another function. Does that mean I would have to make my script like this:
Collapse JASS:
function ReturnFalse takes nothing returns boolean
if(blah) then
    return false
endif
endfunction

function IfThenElse takes nothing returns nothing
if(ReturnFalse() and ReturnTrue()) then
    call blah
endif
endfunction
02-09-2008, 06:38 AM#4
Malf
Don't ever use functions, aside from boolexpr functions, when dealing with booleans/checking stuff.

This is an example of a crappily coded boolexpr function and a clean one.

Collapse Crap:
function Whateva takes nothing returns boolean
    if ( not ( GetPlayerId(GetTriggerPlayer()) == 5 ) ) then
        return false
    endif
    return true
endfunction
Collapse Clean:
function Whateva takes nothing returns boolean
    return GetPlayerId(GetTriggerPlayer()) == 5
endfunction

Both do the same, the first is all messy and has an If-Then-Else, which makes it slower I do believe. The second also checks if the Player's number was 6(Player(5) in JASS is actually Player 6) But it's a whole lot cleaner and look, just one line.
02-09-2008, 02:27 PM#5
Tastingo
ty for the tip, but I'm kind of just wondering how to do And's and Or's. I understand not to use the BJ, but when doing them does it always have to call another function.
02-09-2008, 03:20 PM#6
Ammorth
no, you could do:

Collapse JASS:
if true and true then // true
if true and false then // false
if false and true // false
if false and false // false

if true or false then// true
if false or true then// true
if false or false then// false
if true or true then// true

// hell, you can even do:

if (false or true) and (true or true and false) and false then // false

usually, booleans are returned from other functions, such as IsUnitInGroup() and personal functions such as IsPlayerPlayingAndNotComputer().

Earth-fury was trying to point out if the if statement was true (using an or) before actually checking both sub-statements, it would exit.

Example:
Collapse JASS:
function Check1 takes player p returns boolean
    return IsPlayerPlaying(p)
endfunction

function Check2 takes unit u returns boolean
   return GetWidgetLife(u) > 0.405 // you can use math comparisons in booleans aswell
endfunction

function booleantest takes nothing returns nothing
if Check1(player) and Check2(unit) then // if the first one fails, then the second function won't be checked since the entire thing is already false

if Check1(player) or Check2(unit) then // if the first one is true, then the second function won't be checked since on check is already true (using or)
02-09-2008, 05:04 PM#7
Tastingo
thank you thats exactly wut i needed, but if I'm checking if a character in a substring is "a", then would it return true if a was capitilized, as well?
02-09-2008, 06:06 PM#8
Ammorth
no, "a" != "A" but you can normalize the case with a native:

Collapse JASS:
native StringCase takes string source, boolean upper returns string
02-09-2008, 06:36 PM#9
Tastingo
Ah thank you.