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:
What I can do:
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!"));
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;
}
}
}
7 replies
❔ Convert JSON string to struct
I have a string in JSON format. How can I convert it to a struct?
c++
public struct config
{
public string ip { get; set; }
public int port { get; set; }
}
string _data = @"{""ip"":""127.0.0.1"",""port"":12345""}"; // I want to convert it to a config struct.
c++
public struct config
{
public string ip { get; set; }
public int port { get; set; }
}
string _data = @"{""ip"":""127.0.0.1"",""port"":12345""}"; // I want to convert it to a config struct.
5 replies