Articles Archive
Articles Search
Director Wiki
 

Finding All of an Object's Handlers

June 19, 2001
by Will Turnage

Dear Multimedia Handyman,

Is there a way to find out all of the properties and/or handlers of any given object?

Russell

 

Russell,

The short answer is yes, it is possible to find out all the properties and handlers of an object. Getting a list of properties isn't too difficult, but it requires a lot of discussion about OOP, so I'll save that for a later article all on its own. In the meantime, here's how you can get a list of handlers from a script or object.

The answer lies with the handlers function in Lingo. The handlers function returns a list of symbols which represent all the handlers in a script or object. So if a parent script contained just this code:

on new me

  return me

end

then the handlers function would return [#new]. Using the handlers function in your project is pretty simple. Once you create an object, all you have to do is call the handlers function and Director will list that object's handlers.

gData = new (script "data object")
put gData.handlers ()
-- [#new, #addNum, #subtractNum, #multiplyNum, #divideNum, #updateField]

In this example, you create a new object based on the parent script named "data object". Then you call the handlers function, and Director returns a list of all the handlers in the object.

That's pretty easy, but what if you want to see a list of handlers in an object that hasn't been created yet? You can do this too, but instead of calling an object, you use the handlers function with the script of the member containing your code. For instance,

put member ("data object").script.handlers
-- [#new, #addNum, #subtractNum, #multiplyNum, #divideNum, #updateField]

So now you've got a list of handlers, but you have no idea what to do with them. Well, the possibilities are endless, but one practical use of them is to create a generic behavior that will execute any code inside an object. Check out this sample movie that allows you to perform some simple math on a random number.

Normally, you would have to create a different behavior for each button in this movie to handle all the different math calculations. But a more centralized approach would be to put all of the math functions inside a global object and create one behavior for all the buttons that would just call a different handler in the math object. This generic behavior might have code as simple as this:

property pHandler

global gData


on
mouseUp me

  call (pHandler, gData)

end


on getPropertyDescriptionList

  props = [:]

  props.addProp (#pHandler, [#format: #symbol, #default: 0, #comment: "Select a handler:", #range: member ("data object").script.handlers ()])

  return props

end

When you attach this behavior to one of the button, a dialog box like this pops up:

and you can select which handler you want executed when this button is pressed. The contents of this popup menu come from the getPropertyDescriptionList in the behavior which uses as its range the list of handlers from member ("data object").

A sample Director 8 movie is available for download in Macintosh or Windows format.

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.