#How can I add a function as parameter here in code?
1 messages · Page 1 of 1 (latest)
You just pass it by name
If the signature matches the UnityEvent delegate, you'll be fine
not working
Then either COM_Help is not a method, or its signature does not match
what do you mean by signature, do i have to do something with function?
@surreal musk
Alright, what is the error being given
I think I solved it actually
thx
well I solved it with no parameters, now I'm trying to do Action<T> but I still cant pass it as parameter
it worked as Action without them
never really used lambdas
You're using them right now 
Action is a delegate which accepts no parameters, and returns void
Action<T> accepts parameter T
I did as its shown earler but that does not work either
Action firstAction = FirstMethod;
Action<int> secondAction = SecondMethod; // Action<int> means a method which accepts int. which SecondMethod does
void FirstMethod()
{
}
void SecondMethod(int x)
{
}
Refer to docs that I linked above
idk what I'm doing wrong here
could this implementation in list be a problem?
No. The problem is your lamba syntax
If you don't understand the docs that I linked, I am happy to explain further and answer questions you may have
ok then, am i atleast looking in the right place?
Yes
thats something
That page should have all the information you need to know about how lambdas work. But like I said, if something needs clarifying, I can clarify
Since you're doing a parameter pass-thru, you can just use a method group. i.e. you can replace smth => { COM_Debug(smth); } with simple COM_Debug
Since the signature appears to match
oh so this was that easy
ok let me just see if everythings working
oh yeah its working
thanks so much
Also to answer your earlier question, "what do you mean by signature":
A signature is what differentiates methods. It's composed of the method name, its return type, and its parameter list. For example the following method:
float MultiplyBy2(float value)
{
return value * 2;
}
The signature for this method is float MultiplyBy2(float) - it returns a float, it accepts 1 float parameter, and its name is MultiplyBy2
oh alright
one more thing, if in the same case I would want to pass a function without parameter, can I somehow? Or should I just add some string parameter that wont be used
Action is a delegate whose signature is void _() - a method that accepts no parameters, and does not return a value.
So, you can do this:
Action foo = MyMethod;
void MyMethod()
{
}
You can assign MyMethod to an Action variable, since the signatures align. They both accept no parameters, and they both return void
thats nice
Action<T> same deal, except it accepts a T parameter. So Action<int> can be assigned a method whose signature is void Something(int)
alright, and what about this?
So that requires a different approach
If what you're doing requires a parameter, but you want to give it a method which doesn't have one, you can use a lambda.
For instance, you can't do:
Action<string> foo = MyMethod;
void MyMethod()
{
// code
}
Since foo wants a method that accepts a string, but MyMethod does not accept a string. Lambda solves this:
Action<string> foo = someStr => MyMethod();
The lambda here accepts a string parameter called someStr, but it's not being used. It's just calling MyMethod without passing any arguments.
And in this case, you can discard the parameter with _:
Action<string> foo = _ => MyMethod();
works flawlessly
Cool 👍
thanks for actually teaching me something