Can anyone explain this piece of code for me please ?

I've just started learning how to use Action in Unity, this seem advanced
No description
4 Replies
i like chatgpt
i like chatgpt10mo ago
First, you need to understand variable that points to a method
static void Print(int x)
{
Console.WriteLine(x);
}

static void PrintSquare(int x)
{
Console.WriteLine(x * x);
}

Action<int>? variable_pointing_to_method;

// pointing to Print method
variable_pointing_to_method = Print;

// longer syntax
if (variable_pointing_to_method is not null)
variable_pointing_to_method(10); // prints 10.

// shorter syntax
variable_pointing_to_method?.Invoke(10); // prints 10.



// pointing to PrintSquare
variable_pointing_to_method = PrintSquare;

// longer syntax is removed for the sake of simplicity

// shorter syntax
variable_pointing_to_method?.Invoke(10); // prints 100.
static void Print(int x)
{
Console.WriteLine(x);
}

static void PrintSquare(int x)
{
Console.WriteLine(x * x);
}

Action<int>? variable_pointing_to_method;

// pointing to Print method
variable_pointing_to_method = Print;

// longer syntax
if (variable_pointing_to_method is not null)
variable_pointing_to_method(10); // prints 10.

// shorter syntax
variable_pointing_to_method?.Invoke(10); // prints 10.



// pointing to PrintSquare
variable_pointing_to_method = PrintSquare;

// longer syntax is removed for the sake of simplicity

// shorter syntax
variable_pointing_to_method?.Invoke(10); // prints 100.
Second, you need to understand lambda expression.
Action<int>? variable_pointing_to_method;

// pointing to a lambda expression
variable_pointing_to_method = x => Console.WriteLine(x * x * x);

// longer syntax is removed for the sake of simplicity.

// shorter syntax
variable_pointing_to_method?.Invoke(10); // prints 1000.
Action<int>? variable_pointing_to_method;

// pointing to a lambda expression
variable_pointing_to_method = x => Console.WriteLine(x * x * x);

// longer syntax is removed for the sake of simplicity.

// shorter syntax
variable_pointing_to_method?.Invoke(10); // prints 1000.
The right hand side of the following assignment is a lambda expression.
variable_pointing_to_method = x => Console.WriteLine(x * x * x);
variable_pointing_to_method = x => Console.WriteLine(x * x * x);
You can read x => Console.WriteLine(x * x * x) as a function that takes one argument x of type int and prints the cube of x. Why is the type of x int? Because variable_pointing_to_method is of type Action<int>.
Anu6is
Anu6is10mo ago
to add on to the last part Action<int> expects a method that accepts and int and return nothing aka void method so the lambda x => Console.WriteLine(x * x * x); translates to
static void CubeAndPrint(int x) // void method accepts an int
{
Console.WriteLine(x * x * x);
}
static void CubeAndPrint(int x) // void method accepts an int
{
Console.WriteLine(x * x * x);
}
i like chatgpt
i like chatgpt10mo ago
The previous examples show how to create a local variable pointing to methods which can be a lambda expression too. Now we pass the local variable as an argument of ShowRandom(). This argument is called callback because it is called or invoked in ShowRandom().
Action<int>? localvariable= x => Console.WriteLine($"Hello! {x}");
ShowRandom(localvariable);

// or you can also directly pass without local variable.
ShowRandom(x => Console.WriteLine($"Hello! {x}"));

static void ShowRandom(Action<int>? callback)
{
int rand = Random.Shared.Next(0,10);

callback?.Invoke(rand);
}
Action<int>? localvariable= x => Console.WriteLine($"Hello! {x}");
ShowRandom(localvariable);

// or you can also directly pass without local variable.
ShowRandom(x => Console.WriteLine($"Hello! {x}"));

static void ShowRandom(Action<int>? callback)
{
int rand = Random.Shared.Next(0,10);

callback?.Invoke(rand);
}
The code below is just a trivial wrapping.
ShowCB();

static void ShowCB()
{
ShowRandom(x => Console.WriteLine($"Hello! {x}"));
}

static void ShowRandom(Action<int>? callback)
{
int rand = Random.Shared.Next(0,10);

callback?.Invoke(rand);
}
ShowCB();

static void ShowCB()
{
ShowRandom(x => Console.WriteLine($"Hello! {x}"));
}

static void ShowRandom(Action<int>? callback)
{
int rand = Random.Shared.Next(0,10);

callback?.Invoke(rand);
}
i like chatgpt
i like chatgpt10mo ago
Another non-trivial example.
// customer can subscribe to notification from kitchen staff
// by providing a callback as follows.
Action<int, int>? customerCallback = (x, y) =>

{
Console.WriteLine($"Step {x} of {y} has been completed.");
//other code can go here as well.
};

// customer orders and passes his callback
await OrderPizza(customerCallback);
// customer gets notified every 1 second.


static async Task OrderPizza(Action<int, int>? callback)
{
// one pizza usually takes 5 to 10 steps
// and each step takes about 1 second

// let simulate the number of steps randomly
var steps = Random.Shared.Next(5, 11);

// kitchen staff notifies customer when each step completes by
for (int i = 0; i < steps; ++i)
{
await Task.Delay(1000); // one second delay for simulation purpose
callback?.Invoke(i + 1, steps);// sending both i+1 and steps variables.
}
}
// customer can subscribe to notification from kitchen staff
// by providing a callback as follows.
Action<int, int>? customerCallback = (x, y) =>

{
Console.WriteLine($"Step {x} of {y} has been completed.");
//other code can go here as well.
};

// customer orders and passes his callback
await OrderPizza(customerCallback);
// customer gets notified every 1 second.


static async Task OrderPizza(Action<int, int>? callback)
{
// one pizza usually takes 5 to 10 steps
// and each step takes about 1 second

// let simulate the number of steps randomly
var steps = Random.Shared.Next(5, 11);

// kitchen staff notifies customer when each step completes by
for (int i = 0; i < steps; ++i)
{
await Task.Delay(1000); // one second delay for simulation purpose
callback?.Invoke(i + 1, steps);// sending both i+1 and steps variables.
}
}
No description
Want results from more Discord servers?
Add your server