Using Objects for initialization

While programming my game’s “Entity Framework” as I like to call it, I had the need for a better way to initialize all of my entities. Here are the properties for just the base “Entity” class:

public class Entity extends EventDispatcher{
 
	public static const DISPOSE:String = "dispose_event";
 
	public var pos:Vector;
	public var rotation:Number;
	public var scale:Number;
	public var radius:Number;
	public var isCollidable:Boolean;
	public var team:String;
 
	public var _id:String;
	public var _type:String;
	public var _pos:Vector;
	public var _oldPos:Vector;
	public var _world:GameWorld;
	public var _timePassed:int;
	//...the rest of the code
}

Also I wanted to have default values as well as the ability to load the defaults from some sort of “Settings” file for pretty much all of these values so normally you would have to do your constructor like:

public function Entity(position:Vector, rotation:Number=360, scale:Number=1, radius:Number=1, isCollidable:Boolean=true, team:String="neutral", id:String=null, type:String=null, world:GameWorld=null){
 
	if(pos != null) { pos = position; } else { pos = new Vector(); }
	if(rotation != 360) { rotation = rotation } else { rotation = Settings.EntityRotation; }
	//etc...
 
}

So as you can see that would get pretty messy ESPECIALLY when you decided to extend it since you’d have to put all of those properties in the constructure PLUS any new properties…so you can just imagine the headache there.

Anyways lets just cut to the chase. Here is the wonderful solution I came up with to handle any type of constructor nightmare like that:

public function MyClass(parameters:Object=null){
 
	// .. Defaults ...
 
	someProperty = Settings.defaultPropertyValue;
	anotherProperty = Settings.anotherDefaultProperty;
	//etc...
 
	// .. Now load in any values passed through the parameters object ...
 
	if(parameters){
		for(var property:String in parameters){
			try{
				this[property] = parameters[property];
			} catch(err:ReferenceError){
				trace(" "+ "ERROR!: property \""+ property + "\" does not exist!");
			}
		}
	}
 
	// .. Any other properties not allowd to be set by parameters can be inited here ...
}

This makes it very simple to initialize entities also…as you can see below:

// .. This would suck ...
var entity:Entity = new Entity(null,360,1,1,true,"Player1");
 
// .. Much easier! ...
var entity:Entity = new Entity({_team:"Player1", isCollidable:true});

So there you have it. Hopefully that is as helpful to you as it has been for me thus far. Truly it is just as easy to extend your objects as well by simply copying that little bit of property grabbing code into the constructor and adding a “parameters” object.

Other uses for objects? Hmm well here a few functions that you might find a use for. Simply paste this code into one of your classes to allow modification of even private classes from the outside! I haven’t fully messed with it but was pondering ways of building in some LUA type scripting support into a few of my classes. This might be a start:

public function setProperty(property:String, value:*):void{
	try {
			this[property] = value;
		}
		catch(err:ReferenceError){
			trace("TestClass::setProperty() - ERROR!: Property \""+property+"\" does not exist!");
		}
}
 
public function readProperty(property:String):*{
	try {
		return this[property];
	}
	catch(err:ReferenceError){
		trace("TestClass::readProperty() - ERROR!: Property \""+property+"\" does not exist!");
	}
}
 
public function doMethod(method:String, ...rest):void{
	try{
		this[method].apply(this,rest);
	}
	catch(err:ReferenceError){
		trace("TestClass::doMethod() - ERROR!: Method \""+method+"\" does not exist!");
	}
	catch(err:ArgumentError){
		trace("TestClass:doMethod() - ERROR!: Incorrect argument count on "+ method);
	}
}

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: