Ok I am trying to figure out the best way to invoke a function (in C#) and either pass a variable to it or not. Meaning I want to send both:
MyTest();
MyTest(true);
to:
MyTest(bool bValue){
if (bValue != null){
print(bValue);
}
}
how can I make it so I can send MyTest(); to that function? It requires one argument but I have a very specific case where I cannot provide that argument (because I am using .Invoke and can not pass arguments) but at the same time have a very strong need to pass arguments to the same function.
My current solution is just split it into two functions, One that takes arguments and one that does not. But I don’t like that approach because then I have several functions all doing the same thing.
Guess it depends what version C# unity is using, supposedly 4.0 allows default values to be passed in which would help your case.
Otherwise you could make the variables global so you could reference them inside of MyTest whenever you needed instead of having to pass them in.
I think in version 3 of Unity, they will be adding optional parameters (though I’m not sure how optional parameters work in the context of invoking methods via reflection).
My suggestion is to simply live with the overloads (at least until the 3.0 release) as it is the accepted C# programming style.
EDIT: though, if you were to do this with the future optional parameters, you’d probably make your boolean nullable:
public void MyTest(bool? bValue = null)
{
if (bValue.HasValue)
{
print(bValue.Value);
}
}