- Home /
how to typecast a method holder in Javascript, like Delegate
I started working with threads just to experiment, maybe use it for my pathing, and i was wondering how to typecast methods? also how would you do do a delegate in javascript? or some form of method hoding
I have this working, but want to make a static function I can call from anywhere to get a thread started, or something of the like.
#pragma strict
import System.Threading;
var myBool:boolean=true;
function Start(){
var ts:ThreadStart=new ThreadStart(MyThreadRunner);
var myThread:Thread=new Thread(ts);
myThread.Start();
}
function MyThreadRunner(){
while(myBool){
Thread.Sleep(1000);
Debug.Log("Still going!");
myBool = !myBool;
}
}
BTW: I can replace
var ts:ThreadStart=new ThreadStart(MyThreadRunner);
with
var ts:ThreadStart=new ThreadStart(Answers.PrintWin);
to call a static function PrintWin on class Answers, but how would I typecast that? how would i typecast a var to put in the new TreadStart situation like a delegate? I know I'm repeating the question, just trying to get a clear response.
currently trying to use threads for tasks in editor to prevent a pause during expensive tasks, How do you return to the main thread directly through another threds logic?
cant even use GetComponent in a thread, its unity API, and I think gameObject.transform is an internal Get component
It is indeed an internal GetComponent. I believe delegates don't work in UnityScript, I maybe wrong though. C# ftw ;)
Answer by Peter G · Aug 22, 2011 at 02:28 AM
You can't use delegates in js. Instead they have function types which are like implicit delegates:
var func : Function;
func = SomeFunction;
var func = function () {
Debug.Log("Anonymous Function");
} ;
//callbacks are supported.
var SomeOtherFunction (obj : Object, callback : function(Object) ) {
callback(obj);
}
var someOtherFunc : Function(Object);
someOtherFunc = SomeOtherFunction;
Function types are relatively type safe. They do have to match the signature if you pass them as a callback.
I am mostly self taught programmer through Unity, and mostly just use the unity script reference to expand my scripting vocabulary, and am unfamiliar with some terms. I know delegates hold references to functions, but how are implicit and explicit delegates different? what r call backs? never heard of em, can you give an example
I've taught myself too so my ter$$anonymous$$ology isn't always textbook, but let me give you a definition for these terms. Implicit is usually used to define anything the compiler will do behind the scenes for you. Compare this to explicit which means that you have to manually tell the compiler something.
The most common implicit operation you do is casting from a derived class to a base class or from int to float (and other similar casts).
//example:
var i : int = 4;
var f : float = i;
//the compiler will implicitly cast from int to float without you having to state the cast.
//Compare to the explicit cast:
var i : int = (int)f;
//explicit cast from float to int.
//You could say type inference is done implicitly.
var i = 10;
//This is statically typed. The compiler is able to figure out based on the right side of the "=" that "i" is an int.
I say function types are like implicit delegates because an appropriate delegate is generated automatically. In C#, you must do something like this:
Func<int, int> func = SomeOperation;
Func<int, string> func2 = someOtherOperation;
Generics ease this burden a lot, but with delegates, you need to create each delegate that matches the signature of the method you want to reference. For example, in this case, we have two delegates, both take an int as their parameter, but one outputs and int and the other a string (Func is a built-in delegate in the System namespace useful for operations like this). It can also be tricky to convert one delegate type to another even if they have identical signatures.
But, in js, some of this is done behind the scenes.
var func : Function(int);
//Which delegate did we use?
//The compiler figures it out for us. We just know that It is a delegate that supports a single int as its argument though.
//Then Unity creates a delegate for us that matches the signature of the function we assign to it.
Its similar to type inference. Calling them "Implicit delegates" is really just a good way of describing them to people trying to understand what they are useful for.
The easiest way to describe a callback is passing a function as a parameter to another function to be called. Confuse.... its ok, let me try to give you an example of what a call back does.
(setting the mood). Let's say we have a factory. We have objects being produced and machines to do the various production roles. At each station, an object arrives and a machine is there to do something to it. We call Assemble()
to perform a given operation on the object at the station. The only thing is there are a number of operations that can be performed at any given station. So we create a callback that let's us pass a specific function into the assembly line when we need it.
static function Assemble ( object : Object , action : Function(Object) ) {
//We take an object and the action to perform on the object.
action(object);
//this is a callback. We are calling the function that we passed into function.
}
then we might call it like this:
var widget : Object = new Object();
Factory.Assemble( widget , ConveyorBelt.Transfer );
var secondWidget = new Object();
Factory.Assemble( secondWidget , Furnace.$$anonymous$$old );
So we pass a method into Assemble()
to be called later.
thank you for the in depth answer. $$anonymous$$y current situation is that im looking for the base class of objects om raycasting on, and want to figure out what class is on that object that's extending my base class
Your answer
Follow this Question
Related Questions
GUI calling function with return value. I dont know why this doesnt work. 0 Answers
Identify and Use Unknown Types 1 Answer
Is there any way to check which method called a certain other method? 1 Answer
TypeInferenceRuleAttribute on Resources.LoadAll? 1 Answer
Why use Resources.Load(string path, Type systemTypeInstance);? 2 Answers