AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   true - false (https://forums.alliedmods.net/showthread.php?t=187682)

KamiN 06-16-2012 14:21

true - false
 
Code:

new test = 1
new test = true

Code:

new test2 = 0
new test2 = false

1 and true means the same thing like 0 and false, right?

I mean, you can wirte 1 instead of true and 0 instead of false, yes?

<VeCo> 06-16-2012 14:28

Re: true - false
 
Basically every number except 0, casted to boolean, returns 1 (including negative numbers).
In your code, it will be the same. But usually boolean variables have "bool:" tag to show that they are booleans, not integers, but that isn't necessary.

Backstabnoob 06-16-2012 14:41

Re: true - false
 
Bools and integers are the same. While bools are for better readability, integers can have more options than two, so the usage is situational. If you are sure you will only have the variable to describe a state ( true, false ), you can use bools. If you are however able to put more than two states in one variable, use integers. What I mean is:
PHP Code:

new boolg_LiveRound
new boolg_KnifeRound
new boolg_WarmupRound 

would be worse than
PHP Code:

// let's say 1 is live, 2 is warmup and 3 is knife
new g_Round 

since you can save some memory. If you use enumerations, your code will look even better:

PHP Code:


enum Rounds
{
   
Knife,
   
Warmup,
   
Live
}

new 
Roundsg_Round 


Exolent[jNr] 06-16-2012 15:38

Re: true - false
 
Quote:

Originally Posted by <VeCo> (Post 1729949)
Basically every number except 0, casted to boolean, returns 1 (including negative numbers).

Not true. Casting an integer to boolean does not change its value, just the tag.
So, for example:
Code:
(bool:2 == bool:1) // false (bool:1 == true) // true (bool:0 == false) // true (bool:-1 == true) // false (!!(-1) == true) // true (!!(1) == true) // true (!!(2) == true) // true (!!(0) == false) // true

When you use !, it opposites the bits in the value, so any non-zero number becomes 0 and 0's become 1.

So:
Code:
!(-1) = 0 !(0) = 1 !(1) = 0

When you use it twice, it just does the operation twice.
The result is forcing a value to be a boolean constant.

Code:
!!(2) = !(0) = 1 = true !!(0) = !(1) = 0 = false

<VeCo> 06-16-2012 15:48

Re: true - false
 
Ah, I think I got it now, thanks. I've always wondered what exactly is the purpose of "!!".

fysiks 06-16-2012 17:33

Re: true - false
 
Quote:

Originally Posted by Exolent[jNr] (Post 1729992)
Code:
(!!(0) == true) // true

Code:
!!(0) = !(1) = 0 = false

You should probably fix the latter former.

Exolent[jNr] 06-16-2012 18:37

Re: true - false
 
Quote:

Originally Posted by fysiks (Post 1730061)
You should probably fix the latter.

No, it meant that !!(0) = false, not that the statement wasn't correct, lol.

YamiKaitou 06-16-2012 19:33

Re: true - false
 
Quote:

Originally Posted by Exolent[jNr] (Post 1730085)
No, it meant that !!(0) = false, not that the statement wasn't correct, lol.

If you are saying that !!(0) means false, then how is this correct? (what fysiks was trying to point out)
Code:

(!!(0) == true) // true

fysiks 06-16-2012 23:12

Re: true - false
 
Quote:

Originally Posted by Exolent[jNr] (Post 1730085)
No, it meant that !!(0) = false, not that the statement wasn't correct, lol.

I meant to say "former" instead of "latter".

Exolent[jNr] 06-17-2012 02:55

Re: true - false
 
Quote:

Originally Posted by YamiKaitou (Post 1730131)
If you are saying that !!(0) means false, then how is this correct? (what fysiks was trying to point out)
Code:

(!!(0) == true) // true

Quote:

Originally Posted by fysiks (Post 1730211)
I meant to say "former" instead of "latter".

Sorry, its been fixed.


All times are GMT -4. The time now is 06:07.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.