AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   !! operator (https://forums.alliedmods.net/showthread.php?t=295526)

KiLLeR. 03-28-2017 12:15

!! operator
 
What's mean the "!!" operator?

klippy 03-28-2017 12:32

Re: !! operator
 
It's not a single operator, but negation operator "!" being done twice. You would want to use that when you want to coerce a cell into a boolean, following the rule that 0 = false, anything else = true.
Code:

!!(0) == false
!!(1) == true
!!(42) == true
!!(-69) == true

This way you are making sure that your boolean is either "true" or "false" and not bool:420 for example. That's good because some people prefer to do "if(variable == true)" instead of "if(variable)", which won't give correct results if you don't "normalize" your boolean value.

KiLLeR. 03-28-2017 13:07

Re: !! operator
 
So it will return always 0 or 1?

Thanks you! ;p

PRoSToTeM@ 03-28-2017 13:20

Re: !! operator
 
You can also do "!= 0" to cast integer to bool.

EFFx 03-28-2017 15:03

Re: !! operator
 
PHP Code:

!(g_admin[id]) 

It means if the user is not Admin

PHP Code:

!!(g_admin[id]) 

It means If is user Admin

It just mean the opposite. Am I right?

KiLLeR. 03-28-2017 18:52

Re: !! operator
 
PHP Code:

!!(g_admin[id]) 

As far as i understood it means that will return 0 if is 0 and 1 if is anything different from 0. This is a way to cast integer variable to bool variable.

fysiks 03-28-2017 20:23

Re: !! operator
 
Quote:

Originally Posted by EFFx (Post 2507451)
PHP Code:

!(g_admin[id]) 

It means if the user is not Admin

PHP Code:

!!(g_admin[id]) 

It means If is user Admin

It just mean the opposite. Am I right?

That entirely depends on what g_admin[id] contains. If it is user flags then no, that will not work. You should always use is_user_admin() if you want to know if they are an admin (unless, of course, you fully understand how flags work and you need something different than is_user_admin() provides).

EFFx 03-28-2017 20:47

Re: !! operator
 
Well, before I readed that post I tested if it works and got what you said. It was just an example for explain what I understood. Guess it works only with intergers and booleans.

PRoSToTeM@ 03-28-2017 21:24

Re: !! operator
 
Quote:

Originally Posted by EFFx (Post 2507529)
Well, before I readed that post I tested if it works and got what you said. It was just an example for explain what I understood. Guess it works only with intergers and booleans.

With float too.

fysiks 03-28-2017 21:29

Re: !! operator
 
Using this on a boolean makes absolutely no sense since it's a double negative and will always return the original value. It's only relevant if you are trying to convert a non-boolean to a boolean value.


All times are GMT -4. The time now is 03:17.

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