• 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
3
Question by AlucardJay · Jan 24, 2013 at 01:35 PM · eventlistener

Event Listener for uJS

Is it possible to write an event listener in UnityScript, with the same functionality as CSharpMessenger Extended as found in the Unity Wiki : http://wiki.unity3d.com/index.php?title=CSharpMessenger_Extended

My previous run-in with anything Delagate says to me "no, well not without a major write-around to do the things C# can and uJS cannot".

The only way that I can currently imagine this is to have a singleton that every script can call upon i.e. broadcast to, and in that singleton have a list of every gameObject and string(command) that would normally have AddListener and RemoveListener, then when those scripts call to be added or removed, scan the lists and modify them accordingly. Then when a broadcast function is called on the singleton, SendMessage to the gameObject with the same index as the string in the list. Very rough and poor idea.

To explain my idea another way ....

GUI health bar script would have :

 function OnEnable()
 {
     if ( isPlayerHealthBar )
     {
         MyListener.Instance.StartListening( "PlayerHealth", this.gameObject );
     }
     else
     {
         MyListener.Instance.StartListening( "MobHealth", this.gameObject );
     }
 }
 
 function OnDisable()
 {
     MyListener.Instance.StopListening( this.gameObject );
 }
 
 function ListenerUpdate( theValue : int ) // also functions for theValue as float and string
 {
     someVar = theValue;
 }


PlayerScript would have :

 function Update()
 {
     MyListener.Instance.BroadcastThis( "PlayerHealth", health );
 }


MobScript would have :

 function Update()
 {
     MyListener.Instance.BroadcastThis( "MobHealth", health );
 }


MyListener singleton would have :

 list of gameObjects (GO)
 list of strings with same index as to the gameObject in gameObject list
 
 function StartListening( theFunction : String, theObject : GameObject )
 {
     add theFunction to string list
     add theObject to GO list
 }
 
 function StopListening( theObject : GameObject )
 {
     search GO list for theObject, get the index
     remove item from both lists at index
 }
 
 function BroadcastThis( theFunction : string, theValue : int ) // also functions for theValue as float and string
 {
     search string list for string (theFunction)
     if found, 
         SendMessage to the gameObject in GO list with same index as string .... GO[index].SendMessage( "ListenerUpdate", theValue ); 
 }

My main concern with this idea is the more NPCs, the harder the singleton has to work, for every NPC script in every update is calling MyListener.Instance.BroadcastThis( "SomeString", someVar);

Any thoughts, ideas, suggestions, and/or known uJS resources are most welcome =]

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 AlucardJay · Jan 24, 2013 at 01:43 PM 1
Share

The first real problem I see is that each script is restricted to just one ListenerUpdate function, and then also only one variable unless I use string, int and float making max of 3 variables passable =[

Edit : Thankyou save, you have given me some things to think about.

avatar image save · Jan 24, 2013 at 02:01 PM 0
Share

It's an interesting question! I'm also interested in this type of event listener and I wish I had more info on the subject.

I understand that this isn't what you're looking for but couldn't this be solved with a manager then individual scripts on each GameObject with public variables? Just a quick thought from looking at your example! The manager caches the required components of the GameObjects in lists/built-in arrays. You would then be able to ask the manager for their individual values. Performance-wize this is ridiculous fast and can be done for several objects each frame with almost no impact.

By using a lookup table or other simple id structure (GameObject ID is probably the most optimized or get the script component and ask for an ID previously set by you) you would also be able to randomly pick one object and ask the manager for values. $$anonymous$$akes the object communicate both ways through cached components.

avatar image AlucardJay · Jan 27, 2013 at 04:04 AM 1
Share

This question is also now on the forums as it is probably more a discussion-based question, and I am getting no traffic here apart from save) : http://forum.unity3d.com/threads/167895-An-Event-Listener-for-Unity-JavaScript

Edit : one more bump, then I'll know I have nobody else interested. I can hear the comments now "just use C#" , but where's the challenge in that :P

1 Reply

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

Answer by AlucardJay · Mar 22, 2013 at 11:39 AM

To anyone following this, I had some great suggestions on my forum post : http://forum.unity3d.com/threads/167895-An-Event-Listener-for-Unity-JavaScript

Many thanks to souperdavecdn and dkozar for their replies over there. Special mention to save for their interest also, hope the information on my forum post can help you.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

scripting problems 1 Answer

EventTrigger / EventListener (what's the difference?) 0 Answers

How to handle UnityEvent listeners on instantiated objects 0 Answers

EventListener for Input class, is there any way to do so? 1 Answer

EventSystem: prefabs that aren't in the scene don't accept GameObject 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges