Theres more and more frameworks available for flash developers these days and choosing between them isnt easy, mainly because they all share one feature. No documentation. At least no documentation that you could read to understand what the framework is about unless you like reading API docs.
So, in an effort to get to know more about them the only choice is to try and use them and write about what i find. Im going to start with PixLib as it does have API docs which gives a rough idea of the area it covers and its in active development.
I’ve got real world projects im working on which need some functionality which is available in PixLib. The choice is then, either code it myself or learn the PixLib way. Im going to try it with PixLib and see if it saves me time over the code-it-myself approach (which is what I normally do…I tried ARP before but didnt like the way it was structured so i coded my own micro-architecture which im happy with at the moment)
My first task for PixLib : Loading configuration files.
I need to load in a configuration file at the start of my application and have access to the configuration data throughout my app. I already have my own config file reader which I built it when i just needed a simple key => value mapping but its not good enough anymore.
A quick scan of the API docs reveals a few interesting Classes : Config, ConfigLoader and ConfigLoaderEvent.
Looks like the ConfigLoader is a good place to start and theres a method there which unsurprisingly is called ‘load’.
I’ve already downloaded the PixLib source and unzipped it into my classpath, so now its time to investigate the source 🙂
The constructor of the ConfigLoader takes 2 arguments, an object called config which looks like it will be used to store the config and a deserializer to convert the xml to an object representation. But, before we jump in and start to create the loader one important thing you’ll notice about PixLib is that if you dont supply arguments then it uses sensible defaults. This is great as it saves a lot of wasted time which would be spent looking into other Classes to see how to build them and pass them in etc.
The next vital part of the puzzle is how do we know when the config has been parsed?
Back to the API docs and we can see the ConfigLoader has an onLoadInitEVENT which sounds just like what I need.
So the code i think i’ll need to load the config and get notification will look like this :
configLoader = new ConfigLoader();
configLoader.addEventListener(ConfigLoader.onLoadInitEVENT,this,configLoaded);
configLoader.load();
N.B. I have updated the above code because Francis (the creator of PixLib) kindly pointed out that PixLib automatically creates delegates when you register event listeners. So no need to use an external delegate factory. More bonus points for PixLib.
The ConfigLoader uses a url of ‘config.xml’ by default (im always happy to favour convention) so no need to supply any arguments to the load method.
I’ve added my handler (configLoaded) so now I need to find out how to access my configuration data.
Back to the API, this time to the Config class. Looks like this is a Singleton so I can access it from anywhere and know its the same instance. So just a quick call Config.getInstance() and i’ve got access to my configuration data, converted from xml to a nice round object. Great.
As I said earlier I need a bit more structure in my config than just name – value pairs. Some items need to have properties, some just need to exist.
From looking at the source earlier I saw that the ConfigLoader uses an XMLToObjectDeserializer so im going to go and investigate what happens inside the deserializer.
Right at the top theres some calls to ‘addType’ which seems to be used for mapping types to deserialization routines, but how do I specify those types in my config? (number, string, array among others)
A quick scan of the deserialize function shows
var dataType:String = node.attributes.type;
ah, so I use an attribute called ‘type’ to declare the type of the node, and im guessing that the node value represents the node value (seems reasonable)
Time to edit my config.xml and see if the data comes through how I want it. Here’s what I have so far :
477
277
Then its just a quick check with some trace statements in the config load event handler and we’re done.
trace(Config.getInstance().artifacts.backgrounds.size.width);
Output : 477
Perfect.
How do I think PixLib stood up to the task overall?
It required a bit of investigation and API / source code reading to find out what I needed but it probably took a little less time than building my own AND is a lot more flexible than what I would have built in the time.
Also the PixLib source code is clear and relatively easy to understand. The use of defaults is very well done and makes using the library pretty much painless.
Definitely a thumbs up.
A quick summary for those of you who cant be bothered to read everything.
1. Create a ConfigLoader and load your data
configLoader = new ConfigLoader();
configLoader.addEventListener(ConfigLoader.onLoadInitEVENT,Delegate.create(this,configLoaded));
configLoader.load();
2. Get the Config instance and use your data
mc._width = Config.getInstance().artifacts.backgrounds.size.width
Thanks a lot for this great investigation and tutorial! 🙂
Just a quick note, pixlib’s got a strong event system which auto-builds delegate (with parameters if you need). 😉
So you can do :
configLoader.addEventListener(ConfigLoader.onLoadInitEVENT,this,configLoaded);
instead of:
configLoader.addEventListener(ConfigLoader.onLoadInitEVENT,Delegate.create(this,configLoaded));
Hey Martin, nice to see you putting your hands at pixlib. Didn’t know you had a blog, I just didn’t find a rss feed 🙁
Cheers mate,
Marcelo.
Hey Marcelo, theres an orange XML button up there on the right. That should give you the RSS feed. 🙂
I just gave this a twirl… and noticed that I can “Trace” the value no problem,
but when I try to view the -> Debug -> List Variables it crashes flash.
Nice tutorial, looking forward to hopefully more of them 🙂
Hi,
First : Thanks for this tutorial, it’s very interesting !!!
Second : I’ve a question : When parse it’s finished you get a singleton object and you can retrieve all of the data in your xml. Each data keep the same type that in the xml if an attribute type exist. How many of type exist ?
It’s possible to create a type ? If the type is a personal class, can built an instance of it and passed parameter ?
third : Thanks again !
Hi,
Another question comes in my mind when I’ve read for a second time your tutorial :
you writted : trace(Config.getInstance().artifacts.backgrounds.size.width);
If in you xml you had two nodes call artifacts :
477
277
477
277
How you can get the width of the background of the second artefact ?
Thank you
Hi Sunshine.
For the first set of questions:
1. How many types exist :
To see that you can look in the source code for the standard XMLToObjectDeserializer which declares :
number, string, array, boolean, class and point.
2. Creating types including custom classes with parameters
Yes you can create types, you would have to add them to the deserializer manually before loading your configuration. As for creating instances of your own classes with parameters you can read the post just after this one which covers just that 🙂
For the question in the second comment, I dont think you can do that with the standard deserializer. I think you would have to write your own that would do something like automatically build arrays when it sees repeated node names (thats not a bad idea actually)
I really recommend signing up to the PixLib mailing list as the people on there are very helpful and know much more about PixLib than I do. Im just learning as I go along so I may have missed something and given you a wrong answer.
You can sign up here : http://osflash.org/mailman/listinfo/pixlib_osflash.org
I answered the question about multiple nodes with same name on the pixlib mailing list, but for anyone who is just reading here, the answer is that the deserializer creates property names with incrementing numbers on the end. e.g.
artifacts
artifacts0
artifacts1
etc…
so I was indeed wrong. PixLib already accounts for this situation.
people are stranger