Page 1 of 1

detecting if an optional parameter is given

Posted: Thu Jul 21, 2022 1:09 pm
by Barbie
UScript functions allow optional parameters. Is there a possibility to detect if a parameter was provided? (Like "IsMissing" in VBA or "undefined" property in JS?)

Example:

Code: Select all

function WhatEver(int Param1, optional int Param2) {
	if (IsMissing(Param2))
		DoThis();
	else
		DoSomethingOther();
...

Re: detecting if an optional parameter is given

Posted: Thu Jul 21, 2022 1:15 pm
by Buggie
No, AFAIK.

Partial solution - never send here 0. So if there 0 you can be sure it default value.

Re: detecting if an optional parameter is given

Posted: Fri Jul 22, 2022 9:01 pm
by Dennis
Have you tried: if ( SomeOptionalVariable == none )?

Re: detecting if an optional parameter is given

Posted: Fri Jul 22, 2022 9:24 pm
by Buggie
Did you try it? It is not even compile, just because int can not be None.

Re: detecting if an optional parameter is given

Posted: Sun Jul 24, 2022 2:58 pm
by Feralidragon
Yeah, you cannot detect that, not directly at least, but then again, in most other languages of the same kind you cannot do that either.

In other languages you can define the default value though, and in some you can even define primitive types as nullable, in order to define proper default values for the default behavior that you aim for, which is probably the reason why you would ever want to do this (because maybe 0 is a bad default value for your optional parameter in that function, right?).

However, there is a way for you to achieve what you're aiming for, namely by doing something like this:

Code: Select all

function WhatEver(int Param1)
{
	WhatEverInternal(Param1, 1); //let's say you want "1" to be the default value instead
}

function WhatEverWithParam2(int Param1, int Param2)
{
	WhatEverInternal(Param1, Param2);
}

function WhatEverInternal(int Param1, optional int Param2)
{
	...
}
Basically, you can have 2 function variations, one with Param2 and another without, and then you use whichever one you need.

Re: detecting if an optional parameter is given

Posted: Sun Jul 24, 2022 3:50 pm
by Buggie
Well. Strictly speaking you can make something awful like this:

Code: Select all

function WhatEver(int Param1, optional coerce string strParam2) { // OFC call it with second param as int.
	local int Param2;
	if (strParam2 == "") // Param not set!
		DoThis();
	else
	{
		Param2 = int(strParam2);
		DoSomethingOther(); // use Param2 here
	}