Raised This Month: $32 Target: $400
 8% 

Optional argument


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Boonie
Member
Join Date: Aug 2015
Old 04-19-2017 , 10:58   Optional argument
Reply With Quote #1

Is there any way to have an optional argument?

Code:
public void func(float some[3], float more[3], int args[4], char[] some-optional-arg) {
//stuff
}
Boonie is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 04-19-2017 , 11:07   Re: Optional argument
Reply With Quote #2

yes
PHP Code:
public DoSomething(bool:something false)
{
    if (
something//do stuff
}

DoSomething(true); // something will be true
DoSomething(); // something will be false 

Last edited by 8guawong; 04-19-2017 at 11:08.
8guawong is offline
Boonie
Member
Join Date: Aug 2015
Old 04-19-2017 , 11:10   Re: Optional argument
Reply With Quote #3

I knew you could do it with a bool, but how about with a string.
Boonie is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 04-19-2017 , 11:13   Re: Optional argument
Reply With Quote #4

You can't have default values on public function parameters.
Fyren is offline
sdz
Senior Member
Join Date: Feb 2012
Old 04-20-2017 , 09:08   Re: Optional argument
Reply With Quote #5

You know this is a great time to ask why overloads aren't a thing
sdz is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-20-2017 , 09:23   Re: Optional argument
Reply With Quote #6

Quote:
Originally Posted by EasSidezz View Post
You know this is a great time to ask why overloads aren't a thing
That is a surprisingly complicated question.

Pawn doesn't have types, it has tags - which are intentionally weaker than normal typing (SourcePawn is slowly changing this and promoting tags to real types).

Rather than overloading, upstream Pawn has a feature called tagsets, which allows you to specify a list of potential tags for a function argument. You can then switch on the tag type to implement overloading inside the function (rather than dispatching to one of multiple functions). SourcePawn removed tagsets because they're really silly and require lots of extra syntax to work.

However, Publics can never support overloading for the same reason C doesn't: the mangled name doesn't include typing information (and related to why they do not support default arguments).
__________________

Last edited by asherkin; 04-20-2017 at 09:23.
asherkin is offline
sdz
Senior Member
Join Date: Feb 2012
Old 04-20-2017 , 09:51   Re: Optional argument
Reply With Quote #7

Information that I never knew I needed to know, for some reason i'm not surprised that there's some complex reason behind overloads not being a thing
Thanks for clarifying on that

Last edited by sdz; 04-20-2017 at 09:53.
sdz is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 04-20-2017 , 09:57   Re: Optional argument
Reply With Quote #8

Quote:
Originally Posted by Boonie View Post
I knew you could do it with a bool, but how about with a string.
Through StrEqual(some-opt-arg, <needed string>, <case sensitivity>) or StrContains(some-opt-arg, <string you're looking for>, <case sensitivity>) != -1 if that's the one else == -1 if that's what you're avoiding.
cravenge is offline
Boonie
Member
Join Date: Aug 2015
Old 04-20-2017 , 10:27   Re: Optional argument
Reply With Quote #9

Quote:
Originally Posted by cravenge View Post
Through StrEqual(some-opt-arg, <needed string>, <case sensitivity>) or StrContains(some-opt-arg, <string you're looking for>, <case sensitivity>) != -1 if that's the one else == -1 if that's what you're avoiding.
This is exactly the way I ended up doing it.
Boonie 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 01:29.


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