AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Weird behavior of '?' operator.. (https://forums.alliedmods.net/showthread.php?t=224096)

r0ck 08-20-2013 04:23

Weird behavior of '?' operator..
 
So i tried using '?' operator in normal public functions ( previously i was using in formatex only )

DOESNT COMPILES ( error below from the return line to the '?' operator line )
Code:

error 029: invalid expression, assumed zero
PHP Code:

public SomeFunction(player)
{
    if(!
IsPlayer(player))
        return 
PLUGIN_HANDLED
        
    
(gi_PlayerMoney[player] < 3000) ? (gi_PlayerItem[player] = false) : (gi_PlayerMoney[player] -= 3000)
    
    ..... 
some more code


COMPILES AND WORKS

PHP Code:

public SomeFunction(player)
{
    if(!
IsPlayer(player))
    {
        return 
PLUGIN_HANDLED
    
}
    
    (
gi_PlayerMoney[player] < 3000) ? (gi_PlayerItem[player] = false) : (gi_PlayerMoney[player] -= 3000)
    
    ..... 
some more code


I dont usually put { } for 1 line like return , and never got that prob ...
Its a bug or something ?

Arkshine 08-20-2013 04:57

Re: Weird behavior of '?' operator..
 
Probably a bug compiler but usually you don't use "?" to set variables.

You can either use {} above, or enforcing line end with ";" or making check in a more conventional way "if ... else ...".

r0ck 08-20-2013 05:05

Re: Weird behavior of '?' operator..
 
Quote:

Originally Posted by Arkshine (Post 2017799)
but usually you don't use "?" to set variables.

Yes i know i have not seen in plugins , Thats why i was just testing..

So whats better in setting variables? if ... else... or '?' ?

Thanks

Arkshine 08-20-2013 06:15

Re: Weird behavior of '?' operator..
 
First is probably more reliable.

Shooting King 08-20-2013 07:58

Re: Weird behavior of '?' operator..
 
The Conditional Operator (?:) is generally used to return variables into left operand.

gi_PlayerMoney[player] = (gi_PlayerMoney[player] < 3000)? 0 : (gi_PlayerMoney[player]-3000);

^SmileY 08-20-2013 10:17

Re: Weird behavior of '?' operator..
 
(variable == 1) ? true : false

Warning with this, depends the check maybe return a false positive.

(variable != 2) ? user_kill(id) ? set_user_health(id,100);

and others.

ConnorMcLeod 08-20-2013 11:02

Re: Weird behavior of '?' operator..
 
Seems to be only used to assign a value, so as a function call parameter, or after a '=' sign.


All times are GMT -4. The time now is 15:52.

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