1

I'm new to Unity and I don't know very much about it. However, I've played around with Java and Objective-C in the past; two object-oriented C-based programming languages that are very much like the C# Unity uses.

Objective-C has blocks that are variables that allow you to store a function inside of them and call them later. When used in practice, you can use blocks as function parameters and class properties, saving a programmer "if" statements and instead simply calling the block.

An example of that syntax would be (taken from the linked tutorial site):

// Declare a block variable
double (^distanceFromRateAndTime)(double rate, double time);

// Create and assign the block
distanceFromRateAndTime = ^double(double rate, double time) {
   return rate * time;
};

// Call the block
double dx = distanceFromRateAndTime(35, 1.5);

NSLog(@"A car driving 35 mph will travel %.2f miles in 1.5 hours.", dx);

Java has the Runnable class, which is the basis of its multithreading structure. (Thread inherits from Runnable.) I learned about that class from a previous question I asked. There are two ways of making a Runnable: The Java 8 way of using lambdas...

Runnable r = () -> { System.out.println("This is just an example!"); }

The Java 8 using lambdas as a function parameter...

void someFunction(Runnable r) {
    r.run();
}

// In a method, not sure about the semicolons
someFunction( () -> { System.out.println("This will work, too!"); });

And finally, pre-Java 8...

Runnable r = Runnable(){
    @Override
    public void run() {
         System.out.println("Yet another example");
    }
}

My question is, how would I go about achieving this in Unity's variant of C#?

Community
  • 1
  • 1
DDPWNAGE
  • 1,423
  • 10
  • 37
  • 1
    `Action a = () => Console.WriteLine("lambda action");`, and the asynchronous operation `Task.Run(() => Console.WriteLine("thread pooled"));`. – Mephy May 18 '16 at 21:55
  • @Mephy thank you! I just needed to learn about the `Action` class. Can I put curly brackets around the running code and have a multiline statement? – DDPWNAGE May 18 '16 at 21:58
  • Hi DDPWNAGE. One thing, it's critical to understand that Unity is ***absolutely single threaded***. There are no other threads and you cannot access any other threads. (Note that you may have seen "coroutines". These have utterly no connection to threads in any way.) – Fattie May 19 '16 at 11:12
  • Another problem you may face is that in the first paragraph you mention "object-oriented". It's critical to realise that Unity is absolutely NOT object-oriented, it is an ECS system. It does not have **any concepts even vaguely similar to inheritance** and has utterly nothing to do with OO. (Essay on that http://stackoverflow.com/a/37243035/294884 ) As it happens, the (current) programming language (c#) used to *write behaviors for* Unity GameObects, happens to be, an OO language. But that's irrelevant. They could change tomorrow to using Lisp :) or something and it would not affect Unity. – Fattie May 19 '16 at 11:16
  • @JoeBlow That explains JavaScript. Thank you for the clarification. – DDPWNAGE May 19 '16 at 12:40

2 Answers2

3

The Action delegate would be the equivalent, without multi-threading aspect. You can use an expression by specifying the parameter list, the '=>' symbol and then the resulting expression, which can also be a block. Func can be used instead if the return type is non-void.

Action a = () => Console.WriteLine("a");
Action b = x => Console.WriteLine(x);
Action c = (x, y, z) => {
    Console.WriteLine(x);
    Console.WriteLine(y);
    Console.WriteLine(z);
}
Func<int, int, int> add = (x, y) => x + y; // last type is return-type

Many multithreading libraries will accept actions and funcs as parameters, including the core TPL, which includes method likes Task.Run and Task.ForEach. When operating os collections, the PLINQ extension (Parallel Linq) is a great tool that chains funcs together.

Mephy
  • 2,978
  • 3
  • 25
  • 31
  • 1
    Thank you so much for the clarification! – DDPWNAGE May 18 '16 at 22:05
  • HI @DDPWNAGE althogh you have ticked this answer, I must emphasize again that Unity is ***utterly single threaded***. You appear to be on a totally wrong track. No Unity engineer would ask the question you're asking, if that makes sense. Unity does not have threads. There is absolutely no "thread-like idiom" in Unity. You never think in threads and threads will be totally uninvolved in your Unity working life. – Fattie May 19 '16 at 11:19
  • Not necessarily true. You can use multi-threaded code in Unity3D as long as you don't call the Unity API, which is not thread-safe. – Mephy May 19 '16 at 11:44
2

It is called Action in C#, and you use the lambda operator => :

Action action = () => Console.WriteLine("This is just an example!");

You can also pass it around :

// single line statement doesn't need to be wrapped in a {code block}, and you don't need the `;` at the end
SomeFunction(() => Console.WriteLine("This will work, too!"));

SomeFunction(() =>
{
    //multiline statements
    Console.WriteLine("This will work, too!");
    Console.WriteLine("This will work, too!x2");
    Console.WriteLine("This will work, too!x3");
});

void SomeFunction(Action action)
{
    action();
}
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44