Raised This Month: $ Target: $400
 0% 

Weird behavior of '?' operator..


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
r0ck
Senior Member
Join Date: Jun 2011
Location: India
Old 08-20-2013 , 04:23   Weird behavior of '?' operator..
Reply With Quote #1

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 ?
__________________
Preparing to release my plugins..

Last edited by r0ck; 08-20-2013 at 04:24.
r0ck is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2013 , 04:57   Re: Weird behavior of '?' operator..
Reply With Quote #2

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 ...".
__________________

Last edited by Arkshine; 08-20-2013 at 04:58.
Arkshine is offline
r0ck
Senior Member
Join Date: Jun 2011
Location: India
Old 08-20-2013 , 05:05   Re: Weird behavior of '?' operator..
Reply With Quote #3

Quote:
Originally Posted by Arkshine View Post
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
__________________
Preparing to release my plugins..
r0ck is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2013 , 06:15   Re: Weird behavior of '?' operator..
Reply With Quote #4

First is probably more reliable.
__________________
Arkshine is offline
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 08-20-2013 , 07:58   Re: Weird behavior of '?' operator..
Reply With Quote #5

The Conditional Operator (?:) is generally used to return variables into left operand.

gi_PlayerMoney[player] = (gi_PlayerMoney[player] < 3000)? 0 : (gi_PlayerMoney[player]-3000);
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here

Last edited by Shooting King; 08-20-2013 at 07:59.
Shooting King is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 08-20-2013 , 10:17   Re: Weird behavior of '?' operator..
Reply With Quote #6

(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.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 08-20-2013 , 11:02   Re: Weird behavior of '?' operator..
Reply With Quote #7

Seems to be only used to assign a value, so as a function call parameter, or after a '=' sign.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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