0

Writing the latest patch for Ensemble I came across an obscure issue in Flex.

I have to preface by saying that I hate Actionscript’s lack of support for multiple inheritance – and I should preface that by saying that I love Actionscript. In theory it makes sense: everything just extends something else that extends something else; it’s the way the world works. Except that in some cases we have things which can be any permutation of other things. For example Tom could be a Lawyer, Doctor, Mechanic, Pastry Chef, or any combination of those and without multiple inheritance there’s no good way to model that. Sure, we could make Lawyer extend Doctor, which extends Mechanic, which extends Pastry Chef and we could utilize only the properties and methods we need for Tom, but that’s not really intuitive since Doctors aren’t necessarily Lawyers.

I say all that to give some background on how I came across this Flex issue and to segue into how I begrudgingly worked around the lack of multiple inheritance.

In Ensemble, everything on the map is a Map Sprite and Map Sprites can be any combination of the following: resource (something that yields wood, stone, etc.), structure (something that can be constructed by a builder), builder (something that can construct a structure), unit (something that can attack or be attacked), character (something that can move). In the database, this works simply enough: each of those types has a table, and a Map Sprite just links to whichever of those tables it utilizes, leaving the rest null. On the client this poses a problem as illustrated in the Tom example. To solve this I simply created overlays for unit, structure, etc. that hold all the methods and properties for each of those Map Sprite types (as well as the necessary UIs). I don’t like it, but it works fine and it cleanly separates the code.

Now here’s where the flex issue comes in.

Whereas before the HP UI would bind to map.hero.hp (the [Bindable] hp property on the [Bindable] hero Map Sprite referenced through map), now it binds to map.hero.unit.hp (where unit is the hero Map Sprite’s unit overlay). I forgot to declare the unit overlay with the [Bindable] meta-tag and here was Flex’s result:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at _EnsembleWatcherSetupUtil/setup() at Ensemble/initialize()[C:\path_omitted\Ensemble.mxml:0] at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:2127] at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3396] at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3219] at mx.managers::SystemManager/docFrameListener()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3065]

Absolutely no reference to what line the problem was on or what method actually had the error. With all the other changes I had been making for the latest patch, this was a needle in a hay stack.

After a little over a day of desperate Googling and in a moment taken straight out of House, M.D., irms asked me what changes were made and I immediately knew it had to be with the overlays. Toying around with it for a few minutes I noticed the [Bindable] meta-tag was missing and, sure enough, adding it relieved me of that hideous error.

Leave a Reply