C
C#2y 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!"));
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++
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;
}
}
}
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;
}
}
}
4 Replies
HowNiceOfYou
HowNiceOfYou2y ago
You can modify the wait method to accept an object[] argument for the arguments to be passed to the Action delegate.
public static void wait(float duration, Action<object[]> func, object[] args)
{
for (int i = 0; i < 100; i++)
{
if (Waits[i].isInUse == false)
{
Waits[i].isInUse = true;
Waits[i].waitTime = duration;
Waits[i].waitFunc = () => func(args);
break;
}
}
}
public static void wait(float duration, Action<object[]> func, object[] args)
{
for (int i = 0; i < 100; i++)
{
if (Waits[i].isInUse == false)
{
Waits[i].isInUse = true;
Waits[i].waitTime = duration;
Waits[i].waitFunc = () => func(args);
break;
}
}
}
Now you can call it like this...
wait(3, test, new object[] { "Hello World!" });
wait(3, test, new object[] { "Hello World!" });
And you can use the test method as such
static void test(object[] args)
{
Console.WriteLine((string)args[0]);
}
static void test(object[] args)
{
Console.WriteLine((string)args[0]);
}
@Moonstalker
entasy
entasyOP2y ago
Thank you a lot @HowNiceOfYou
HowNiceOfYou
HowNiceOfYou2y ago
Yeah man, anytime.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server