#How can I add a function as parameter here in code?

1 messages · Page 1 of 1 (latest)

near niche
surreal musk
#

You just pass it by name

#

If the signature matches the UnityEvent delegate, you'll be fine

near niche
#

not working

surreal musk
#

Then either COM_Help is not a method, or its signature does not match

near niche
#

what do you mean by signature, do i have to do something with function?

#

@surreal musk

surreal musk
#

Alright, what is the error being given

near niche
#

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

surreal musk
#

Well, yeah

#

Because you're missing a parameter on the lambda...

near niche
#

never really used lambdas

surreal musk
#

You're using them right now lolwutvelma

near niche
#

theres always the first time

#

but anyways

surreal musk
#

Action is a delegate which accepts no parameters, and returns void

#

Action<T> accepts parameter T

near niche
#

I did as its shown earler but that does not work either

surreal musk
#
Action firstAction = FirstMethod;
Action<int> secondAction = SecondMethod; // Action<int> means a method which accepts int. which SecondMethod does

void FirstMethod()
{
}

void SecondMethod(int x)
{
}
surreal musk
near niche
#

idk what I'm doing wrong here

surreal musk
#

You're guessing, that's what

#

Refer to docs

near niche
#

could this implementation in list be a problem?

surreal musk
#

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

near niche
#

ok then, am i atleast looking in the right place?

surreal musk
#

Yes

near niche
#

thats something

surreal musk
#

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

near niche
#

ok so now there is no error

#

but

#

what now

#

i dont need this smth

surreal musk
#

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

near niche
#

oh so this was that easy

#

ok let me just see if everythings working

#

oh yeah its working

#

thanks so much

surreal musk
# near niche what do you mean by signature, do i have to do something with function?

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

near niche
#

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

surreal musk
#

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

near niche
#

thats nice

surreal musk
#

Action<T> same deal, except it accepts a T parameter. So Action<int> can be assigned a method whose signature is void Something(int)

surreal musk
#

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();
near niche
#

works flawlessly

surreal musk
#

Cool 👍

near niche
#

thanks for actually teaching me something