Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Techsuport · Aug 12, 2011 at 02:07 PM · functionmethodtypethreadtypecasting

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Techsuport · Aug 19, 2011 at 10:50 AM 0
Share

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?

avatar image Techsuport · Aug 19, 2011 at 11:03 AM 0
Share

cant even use GetComponent in a thread, its unity API, and I think gameObject.transform is an internal Get component

avatar image Joshua · Aug 22, 2011 at 02:03 AM 0
Share

It is indeed an internal GetComponent. I believe delegates don't work in UnityScript, I maybe wrong though. C# ftw ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Techsuport · Aug 23, 2011 at 06:17 PM 0
Share

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

avatar image Peter G · Aug 24, 2011 at 02:39 AM 4
Share

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.

avatar image Peter G · Aug 24, 2011 at 02:52 AM 3
Share

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.

avatar image Techsuport · Sep 01, 2011 at 11:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges