- Home /
public variable listener for dispatch in composed script
Hi,
I am using a script as a middle man between the inspector interface and other scripts ( for various reasons like common setup, user friendly variable that means several changes in various scripts, etc).
the problem is how can I listen to a public variable ( of its own script), I would like to avoid having to do checks in the update() function, but maybe this is the only way...
Any tip welcome,
Thanks,
Jean
Answer by Bampf · Aug 19, 2010 at 01:23 PM
There's no built-in facility to listen for changes to a variable. Here are two suggestions for "pushing" changes to listeners.
- A good technique for this kind of problem is for the listeners to "register" with the middleman script. They tell the script what events or changes they are interested in, and then the middleman script notifies the listeners when things happen.
The middleman script basically becomes your own event manager. Rather than write this from scratch, have a look at sample implementations in the Unify wiki. Events can be whatever you define. "Game ended", "Player lost a life", "Music volume changed", low level events like taps or clicks, etc.
This may seem like overkill for what you asked, but once you have such a system in place you will find it solves problems for you that you didn't see coming. For example, on my current project I decided fairly late to add tutorial messages that change depending on what has just happened in the game. If I'd had one of these event queues in place it would have been easy and clean.
- A cheap alternative is to make the variable a C# property. Inside the set{} block you trigger any necessary side-effects, by sending messages or calling methods of other objects/scripts. No matter how the variable is set, the side-effects will get triggered. (You make the actual variable private to force all access to go through the property.
Straightforward, but you'd be hardwiring calls to the other scripts that are being notified of changes. You might be able to use SendMessage or BroadcastMessage to make that logic more generic. Then the middeman script could allow other objects to register themselves as listeners, asking to have a certain message sent to them when the variable changes. Now your solution is starting to look like option 1, though for a single variable.
Hi Bampf, thanks for this, very informative and lots to learn and digest. the more I get into Unity the more I feel that I should switch to csharp to write scripts as it "seems" to have easier access to advanced coding technics. Am I correct?
It's a matter of debate. I use C# myself. When learning something like Unity, I am always asking "what is the type of the object I got back?" It's just how I remember things. Also you get more compile-time checking. If you come from a C++/C# background, I'd recommend it. However, Unity's syntax in Javascript is quite elegant, and some things take less code. Documentation examples are in JavaScript too.
Your answer
Follow this Question
Related Questions
change variable name inside IF statement, or add text to it 3 Answers
Public variable not showing in Inspector(Solved) 1 Answer
Passing Parameters by Value or by Reference 2 Answers
How do I specify a public keyboard-character variable in a script? 1 Answer
Public Texture Variables Not Working 0 Answers