Home > Software engineering >  how can i pass the function into the function?
how can i pass the function into the function?

Time:04-24

the original code like below,how can i pass the function "Method();Method2();" like using (GameObject:GO)

void passfunction(GameObject:GO){
if (Input.touchCount == 1)
        {
            Touch screentouch = Input.GetTouch(0);
            Ray ray = firstcamera.ScreenPointToRay(screentouch.position);
            if (screentouch.phase == TouchPhase.Began)
            {
                if (Physics.Raycast(ray, out RaycastHit hitInfo))
                {
                    if (hitInfo.collider.gameObject == GO)
                    {
                        Method(); // pass this function
                        Method2();// pass this function
                    }
                }
            }
        }
};

CodePudding user response:

You can use Action and try this.

using System;

public void Method1()
{
    passfunction(GO,Method,Method2);
}

public void passfunction(GameObject:GO,Action Method,Action Method2)
{
    if (Input.touchCount == 1)
        {
            Touch screentouch = Input.GetTouch(0);
            Ray ray = firstcamera.ScreenPointToRay(screentouch.position);
            if (screentouch.phase == TouchPhase.Began)
            {
                if (Physics.Raycast(ray, out RaycastHit hitInfo))
                {
                    if (hitInfo.collider.gameObject == GO)
                    {
                        Method(); // pass this function
                        Method2();// pass this function
                    }
                }
            }
        }   // This will call the method that has been passed as parameter
}

CodePudding user response:

fei-nathanlin's answer is correct, but since I can't comment I'll expand on it in an answer to show how to pass around functions with any signature, not just ones that return void and don't accept parameters.

(This is an expansion on an already existing answer, so I didn't include the original code as that has already been solved.)

Functions as Parameters

You can use C#'s delegate type to pass functions to other functions, or you can use the generic delegate types provided by C#: Action & Func.

Here is a table showing the differences between them:

Action Func
Return Type void Any type that isn't void
Parameters Min: 0, Max: 16 Min: 0, Max: 16

As you can see, they are incredibly similar - the only real difference between them is the return type that they allow.
(Action also has a non-generic overload that accepts functions that return void and don't have parameters, whereas Func does not.)

Since generic types must be known at compile-time, you have to specify the signature (The Return Type & Parameter Type(s), or lack thereof) of the passed method in the function you're passing it to.

That means if you want to make a function that accepts both of these methods:

public void Example1()
{
    Console.WriteLine("This method returns void, and has no parameters.");
}
public bool Example2()
{
    Console.WriteLine("This method returns bool, and has no parameters.");
    return false;
}

You're going to have to overload the receiving function so that you can pass Example1 as an Action, and Example2 as a Func<bool>:

public void CallExampleMethod(Action method)
{
    method();
}
public void CallExampleMethod(Func<bool> method)
{
    bool result = method();
}

Making your own Delegates

//              ▼▼▼▼ Accepts functions that return void, and-
public delegate void MyDelegate();
//                             ▲▲ -don't have any parameters.

//              ▼▼▼▼ Accepts functions that return bool, and-
public delegate bool MyOtherDelegate(string s);
//                                   ▲▲▲▲▲▲ -have 1 parameter of type string

The equivalent of MyDelegate is Action, and the equivalent of MyOtherDelegate is Func<string, bool>.

You can read more about delegates here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates

  • Related