PBE Series: dynamically get list of public members, properties and methods of object or class

December 27th, 2009 by Christian Leave a reply »

Now this isn’t necessarily only related to PushButton Engine. Without using the Logger functions, it would also work with “plain” ActionScript. So what do I want to do here? My main problem was, with Level Master 2000, I want to have UI-based component composition. And each component has different properties which should be settable in the editor. And it looks like this is pretty easy to do with ActionScript. All you need to do is get an XML description of your object or class, by using the built in function describeType:

//Produces an XML object that describes the ActionScript
//object named as the parameter of the method:
var varList:XML = flash.utils.describeType( /*INSERT OBJECT OR CLASS*/ );

And then, depending on what you want to access (properties, members or functions), just iterate over your XML and you’re good!

Logger.print(this, "Properties:");
for each (var a:XML in varList.accessor)
{
	Logger.print(this, a.@name+" : "+a.@type);
}
Logger.print(this, "Variables:");
for each (var b:XML in varList.variable)
{
	Logger.print(this, b.@name+" : "+b.@type);
}
Logger.print(this, "Methods:");
for each (var c:XML in varList.method)
{
	Logger.print(this, c.@name+" : "+c.@type);
}

For me with my editor, I’ll probably do this once at startup and keep the information for all the components stored somewhere. I currently don’t know how performance is, but I guess it’s gonna be better that way. If it turns out to be a wrong approach, I’ll post my conclusion here ;)

Edit – doing it the PushButton way:
Of course there seems to be a much easier way doing it with PushButton Engine. I must admit, I haven’t tried it, but it seems there’s this built in function which sort of dumps all classes and public info to a xml file by just using:

SchemaGenerator.instance.generateSchema();

Nice!

1 comment

  1. Christian says:

    Regarding the SchemaGenerator.instance.generateSchema(), I’ve tested it now and it doesn’t quite work that way. I’ll post a new tutorial how to use this soon!

Leave a Reply