Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Default117 · Nov 20, 2011 at 11:43 PM · javascriptprime31delegates

JS Delegates

Hey guys, Can someone please point me in the right direction to get delegates and events to work in JavaScript/UnityScript? I usually program in C#/C++ but for this project we're required to work in JS. I've followed Prime31's youtube video but still cannot get it to work.

Major issues I'm having is issues with unity not recognising the "delegate" and "event" tokens. Even trying to simplify it down to just having a function, and a static variable of that function as a type Unity still gives an unexpected token error message.

Thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 21, 2011 at 12:00 AM

As far as i know you have to use the "usual" Javascript syntax (the type "Function" with a capital F). As you might know even UnityScript is compiled to a .NET / Mono dll. UnityScript is not a standardized language and i only use C# for Unity so i can't tell the exact syntax but i think i can remember a question about that issue and it wasn't that difficult.

I would try something like:

 var myDelegate : Function(parameter : float);

but haven't tested it yet ;)

Comment
Add comment · Show 7 · 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 Default117 · Nov 21, 2011 at 12:02 AM 0
Share

Thanks i'll have to try that out. I do remember reading some pseudo code using Function with a capital F, but i didn't realise that this is the "type" function. C# Syntax would be something like:

 public delegate void functionHandler( string data );
 public static event functionHandler newCallback1; 
 public static event functionHandler newCallback2;

I'll try something similar with your suggestion above =)

avatar image Bunny83 · Nov 21, 2011 at 12:29 AM 0
Share

Sure, but i don't think UnityScript have something like the "event" modifier. Even in C# it's "just" a way to restrict the invocation to the containing class and enables you to declare it in an interface declaration. Since interfaces is one of the things that can't be defined (but used) in UnityScript the event keyword becomes almost useless.

See this article about delegates and events

avatar image Default117 · Nov 21, 2011 at 12:31 AM 0
Share

Unfortunately this suggestion does not work entirely. I got everything working up to this point: var $$anonymous$$yDelegate : function( String );

 static var firstCallBack : Function;
 firstCallBack = $$anonymous$$yDelegate;
 static var secondCallBack : Function;
 secondCallBack = $$anonymous$$yDelegate;

But when i try to add the callbacks to my listener through the normal syntax of firstCallBack += newFunction; It spits the dummy.

avatar image Bunny83 · Nov 21, 2011 at 01:01 AM 1
Share

Unfortunately you can't create a delegate-type in UnityScript (AFAI$$anonymous$$). In Javascript there are only something like function pointers. I've found the "old" question on this issue ;)

I'm not sure if UnityScript supports multicast delegates. Personally i don't really care about that since it's not my language, sorry ;)

$$anonymous$$aybe one of the pro UnityScript users can tell you more about that, but it seems that's one of those features which are not completely implemented in US.

avatar image Peter G · Nov 21, 2011 at 01:55 AM 3
Share

An interesting feature I found. JS can't create events, but it can add its function type objects to events created by C# scripts. Also, js supports creation of anonymous functions and you can assign them to any compatible function pointer.

 //C#
 public class Blah {
       public event eventHandle trigger;
 }

------

 //js
 var instance : Blah;
 var myFunc : Function = function() { Debug.Log("Anonymous $$anonymous$$ethod"); };
 instance.trigger += myFunc;
Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to do c# Action in UnityScript 0 Answers

c# delegates/events using from js? 1 Answer

UnityScript, Delegates and SmoothMoves 1 Answer

Difference between Function and function() Types 1 Answer

Setting Scroll View Width GUILayout 1 Answer


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