C#C
C#3y ago
entasy

❔ ✅ Help Passing Arguments to Action (Custom Wait Code)

Hi, I'm trying to code a custom wait code.
This code works, but I want to use methods with arguments.

What I want:
c++
static void test(string text){
    Console.WriteLine(text);
}
wait(3, test("Hello World!"));

What I can do:
c++
static void test(){
    Console.WriteLine("Hello World!");
}
wait(3, test); // It shows "Hello World!" message after 3 seconds.

c++
public static void Main(string[] args)
{
    wait_init();

    new Thread(
        () => { while (true) { wait_tick(); Thread.Sleep(100); } }
    ).Start();

    static void test()
    {
        Console.WriteLine("Hello World!");
    }
    wait(3, test);
}

public static WaitStruct[] Waits = new WaitStruct[100];

public struct WaitStruct
{
    public bool isInUse;
    public float waitTime;
    public Action waitFunc;
}

public static void wait_init()
{
    for (int i = 0; i < 100; i++)
    {
        Waits[i].isInUse = false;
        Waits[i].waitTime = 0;
    }
}

public static void wait_tick()
{
    for (int i = 0; i < 100; i++)
    {
        if (Waits[i].isInUse == true)
        {
            Waits[i].waitTime -= 0.1f;
            Console.WriteLine(Waits[i].waitTime);
            if (Waits[i].waitTime <= 0){
                Waits[i].isInUse = false;
                Waits[i].waitFunc();
            }
        }
    }
}

public static void wait(float duration, Action func)
{
    for (int i = 0; i < 100; i++)
    {
        if (Waits[i].isInUse == false)
        {
            Waits[i].isInUse = true;
            Waits[i].waitTime = duration;
            Waits[i].waitFunc = func;
            break;
        }
    }
}
Was this page helpful?