Raised This Month: $ Target: $400
 0% 

wtf does this mean?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nergal
Veteran Member
Join Date: Apr 2012
Old 03-10-2014 , 23:02   wtf does this mean?
Reply With Quote #1

i've never seen ANYTHING in the pawn tutorial about this....

PHP Code:
variable + (20); 
Specifically, idk what the "int ? int : int" part means
__________________

Last edited by nergal; 03-10-2014 at 23:07.
nergal is offline
arthurdead
Senior Member
Join Date: Jul 2013
Old 03-10-2014 , 23:12   Re: wtf does this mean?
Reply With Quote #2

Quote:
Originally Posted by nergal View Post
i've never seen ANYTHING in the pawn tutorial about this....

PHP Code:
variable + (20); 
Specifically, idk what the "int ? int : int" part means
if 2 is less then 4 1 + 5 else 1 + 20 (i might be wrong)
arthurdead is offline
psychonic

BAFFLED
Join Date: May 2008
Old 03-10-2014 , 23:14   Re: wtf does this mean?
Reply With Quote #3

It's a ternary operator.

https://en.wikipedia.org/wiki/%3F:
psychonic is offline
nergal
Veteran Member
Join Date: Apr 2012
Old 03-10-2014 , 23:42   Re: wtf does this mean?
Reply With Quote #4

Quote:
Originally Posted by psychonic View Post
It's a ternary operator.

https://en.wikipedia.org/wiki/%3F:
Thanks for clearing that up Psychonic, but why doesn't the sourcepawn tutorial cover this?

This would've been useful if I had read it from the Conditionals portion of the SourcePawn Intro wiki article.
__________________
nergal is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 03-11-2014 , 04:05   Re: wtf does this mean?
Reply With Quote #5

Tenary operators shouldn't be overused because they decrease the readability in favor of code lines.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-11-2014 , 09:48   Re: wtf does this mean?
Reply With Quote #6

But If statement nests can ruin the look of code also
Mitchell is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-11-2014 , 11:21   Re: wtf does this mean?
Reply With Quote #7

Quote:
Originally Posted by Mitchell View Post
But If statement nests can ruin the look of code also
Sometimes those are a necessary evil.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-11-2014 , 12:14   Re: wtf does this mean?
Reply With Quote #8

Quote:
Originally Posted by Powerlord View Post
Sometimes those are a necessary evil.
-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:
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!");
}
instead of:
Code:
PrintToChat(client, "You are a %s this round!", (PlayerSettings[client][MClass] == MatMClass_Traitor) ? "Traitor" : (PlayerSettings[client][MClass] == MatMClass_Detective) ? "Detective" : "Innocent");
Mitchell is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-11-2014 , 12:18   Re: wtf does this mean?
Reply With Quote #9

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:
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!");
}
instead of:
Code:
PrintToChat(client, "You are a %s this round!", (PlayerSettings[client][MClass] == MatMClass_Traitor) ? "Traitor" : (PlayerSettings[client][MClass] == MatMClass_Detective) ? "Detective" : "Innocent");
...those aren't quite identical due to an for innocent in the first example.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 03-11-2014 at 12:18.
Powerlord is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-11-2014 , 12:41   Re: wtf does this mean?
Reply With Quote #10

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.
__________________
asherkin 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 11:46.


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