Raised This Month: $12 Target: $400
 3% 

wtf does this mean?


Post New Thread Reply   
 
Thread Tools Display Modes
Mitchell
~lick~
Join Date: Mar 2010
Old 03-11-2014 , 13:02   Re: wtf does this mean?
Reply With Quote #11

Quote:
Originally Posted by asherkin View Post
While, yes, that if..elseif..else list is ugly, there is magical syntax to help for that case:

PHP Code:
switch (PlayerSettings[client][MClass])
{
  case 
MatMClass_Traitor:
    
PrintToChat(client"You are a traitor this round!");
  case 
MatMClass_Detective:
    
PrintToChat(client"You are a detective this round!");
  default:
    
PrintToChat(client"You are an innocent this round!");

Which, imo, looks considerably better than both, and still covers the a/an case.
Yes, i agree looks alot better than a bunch of ifs.
covering the 'an' for innocent.
Code:
PrintToChat(client, "You are a%s this round!", (PlayerSettings[client][MClass] == MatMClass_Traitor) ? " Traitor" : (PlayerSettings[client][MClass] == MatMClass_Detective) ? " Detective" : "n Innocent");
Mitchell is offline
turtsmcgurts
SourceMod Donor
Join Date: Jul 2011
Old 03-11-2014 , 13:44   Re: wtf does this mean?
Reply With Quote #12

Quote:
Originally Posted by Mitchell View Post
-snip-
i was going to show an example in python, but nvm all i would be doing is changing a one line to 3 lines..

It's better to break up if-statements into methods to handle them, making the code look less bulky in one huge functions.

I just hate having to do:

instead of:

Assuming what you want is exactly like you posted (one liners), this looks far better imo.

Code:
if(PlayerSettings[client][MClass] == MatMClass_Traitor)
	PrintToChat(client, "You are a traitor this round!");
else if(PlayerSettings[client][MClass] == MatMClass_Detective)
	PrintToChat(client, "You are a detective this round!");
else
	PrintToChat(client, "You are an innocent this round!");
turtsmcgurts is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-11-2014 , 13:55   Re: wtf does this mean?
Reply With Quote #13

Ternaries tend to be harder to read. You could (at the cost of a variable) do something like this:

PHP Code:
decl String:bacon[15];

switch (
PlayerSettings[client][MClass])
{
  case 
MatMClass_Traitor:
    
bacon " traitor";
  case 
MatMClass_Detective:
    
bacon " detective";
  default:
    
bacon "n innocent";
}

PrintToChat(client"You are a%s this round!"bacon); 
Or more likely using Format commands with %T because you'd be using translation phrases, right?
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 03-11-2014 at 14:04.
Powerlord is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-11-2014 , 14:57   Re: wtf does this mean?
Reply With Quote #14

Quote:
Originally Posted by Powerlord View Post
Ternaries tend to be harder to read. You could (at the cost of a variable) do something like this:

PHP Code:
decl String:bacon[15];

switch (
PlayerSettings[client][MClass])
{
  case 
MatMClass_Traitor:
    
bacon " traitor";
  case 
MatMClass_Detective:
    
bacon " detective";
  default:
    
bacon "n innocent";
}

PrintToChat(client"You are a%s this round!"bacon); 
Or more likely using Format commands with %T because you'd be using translation phrases, right?
yeahh.. i still need to look up on doing that with color chat for moar customization.
Mitchell is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 03-12-2014 , 02:41   Re: wtf does this mean?
Reply With Quote #15

Magic operator that lets you initialize variables without declaring needless intermediate variables.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
captaindeterprimary
AlliedModders Donor
Join Date: Sep 2012
Old 03-12-2014 , 10:05   Re: wtf does this mean?
Reply With Quote #16

wtf = What the fudge
? = a punctuation mark (?) indicating a question
__________________
Last edited by ; Today at 08:20 AM. Reason: Get rid of s
captaindeterprimary is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 03-15-2014 , 07:03   Re: wtf does this mean?
Reply With Quote #17

It's fine to use ternary operators, as long as you're using them "correctly". They are useful for selecting one of two values:
PHP Code:
new bool:speedEnabled IsSpeedEnabled();
new 
speedBoost GetSpeedBoost();
new 
defaultSpeed GetDefaultSpeed();

new 
speed speedEnabled speedBoost defaultSpeed
Note that I only refer to variables. Avoid doing function calls on the line using the ternary operators, otherwise you'll do two things on the same line and disrupt readability.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 03-15-2014 at 07:06.
rhelgeby is offline
Send a message via MSN to rhelgeby
nergal
Veteran Member
Join Date: Apr 2012
Old 03-18-2014 , 10:09   Re: wtf does this mean?
Reply With Quote #18

Quote:
Originally Posted by rhelgeby View Post
It's fine to use ternary operators, as long as you're using them "correctly". They are useful for selecting one of two values:
PHP Code:
new bool:speedEnabled IsSpeedEnabled();
new 
speedBoost GetSpeedBoost();
new 
defaultSpeed GetDefaultSpeed();

new 
speed speedEnabled speedBoost defaultSpeed
Note that I only refer to variables. Avoid doing function calls on the line using the ternary operators, otherwise you'll do two things on the same line and disrupt readability.
expanding on this, what if I used them for menus?

PHP Code:
public MenuHandler_SetLulz(Handle:menuMenuAction:actionclientparam2

    
decl String:menuitem[64]; 
    new 
int
    
GetMenuItem(menuparam2menuitemsizeof(menuitem)); 
    if (
action == MenuAction_Select
        { 
                
param2++; 
                
int = (param2 == 1) ? : ((param2 == 2) ? : ((param2 == 3) ? 0)); 
                
PrintNumber(clientint); 
    } 
    else if (
action == MenuAction_End
        { 
        
CloseHandle(menu); 
    } 

__________________

Last edited by nergal; 03-18-2014 at 10:10.
nergal is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 03-18-2014 , 11:21   Re: wtf does this mean?
Reply With Quote #19

PrintToChat(iClient,"You is %s",(IsFakeClent(iClient) == true) ? "BOT" : "PLAYER");

You can compare variables (Like an IF) but implementing directly in code, Ah yeah you can concatenate strings too.

(Condition) ? true_value : false_value
__________________
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
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 03-18-2014 , 14:18   Re: wtf does this mean?
Reply With Quote #20

Quote:
Originally Posted by nergal View Post
PHP Code:
int = (param2 == 1) ? : ((param2 == 2) ? : ((param2 == 3) ? 0)); 
I would avoid nesting them, use if-statements in this case (or perhaps a switch-statement). Code readability is more important than saving some lines.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)
rhelgeby is offline
Send a message via MSN to rhelgeby
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 14:52.


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