After learning recently that Lubos Lenco was using my Composure library as the foundation of his impressive ZBlend game framework, I decided to add a few more really helpful features.

Promises

Sometimes when using @inject to inject multiple properties, it’s helpful to know when all of the properties have been injected. This can be done by adding the @promise metadata to a method with a list of the properties that should be watched for. The method will be called immediately after all properties get set.

Notice that it is possible to use non-injected variables in your promises (‘active’ in the below example). At compile-time these variables will be wrapped in getter/setters to facilitate the watching process (or the setter will be amended if it already has one).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    class MainPlayer extends AbstractTrait{

        public var active:Bool;

        @inject({asc:true})
        private var gamepad:IInputDevice;

        @inject
        private var controller:CharacterMotionControl;

        public function new(){
            super();
        }

        @promise("active", "gamepad", "controller")
        public function onPromiseMet(met:Bool):Void{
            if(met){
                trace("Both traits have been added and are ready to bind together");
                controller.setInput(gamepad);
            }else{
                trace("One of the traits is about to be removed, unbind them now");
                controller.setInput(null);
            }
        }
    }

How promises behave

  • The first call to your bound method will be called as soon as all applicable variables have been injected, the met parameter will always be true on the first call.
  • If any of the properties get set to another value, then the method will be called twice, once before the property is committed with met=false, then again after the value has been set with met=true. This will really only happen when promises are pointing to non-injected properties (which are set outside of the composure framework).
  • If any of the applicable traits are removed (i.e. the property is set to null) then the method will be called again with met=false

Future features

There are a few other ideas floating around for more features and improvements, which I’ll have a look at when I get a chance:

  • Compile-time binding for certain pre-established objects. This will make things much faster and lighter when generating lots of the same type of game objects (a bunch of enemies, for example).
  • A light and simple event system for communicating events between the traits of a ComposeItem. This would also come with metadata support.
  • A simpler, lighter version of the @inject metadata that trades some flexibility for performance.