Init parameters for entities?
Hiya, Im getting into Crafty atm and really enjoying it - but Im stumped on something that Im hoping is just me missing a trick.
I have some code like this;
Crafty.c("turret", {
shoot:function(){
Crafty.e('bullet')
.attr({
x: this._x + some_math,
y: this._y + some_math,
//rotation is set here but it's too late
//as init already called
rotation: this._rotation
});
}
});
Crafty.c("bullet", {
init:function(){
var bulletSpeed 100;
//I want to init the speed here but
//how to pass turret rotation in?
this.xspeed = bulletSpeed * Math.sin(this._rotation);
this.yspeed = bulletSpeed * Math.cos(this._rotation);
this.bind("EnterFrame", function(params) {
this.x += this.xspeed;
this.y -= this.yspeed;
});
}
});
So you can see my problem is I want to set bullet direction based upon turret at fire time. I'd like to be able to do something like;
Crafty.e('bullet',{
x: this._x + some_math,
y: this._y + some_math,
rotation: this._rotation
});
But it seems like there's no way to pass parameters into the contructor/init method. The best workaround I could see was to define an "after init" method to be called like;
Crafty.e('bullet')
.afterInit({
x: this._x + some_math,
y: this._y + some_math,
rotation: this._rotation
});
But thats inelegant as it's just cludging a second init method. So - am I missing a cleaner way to do this? Or would this be something you'd be open to adding?
Cheers, Greg
You're doing it wrong -- you need a constructor function.
Crafty.c('bullet', {
init: function() {
this.requires('2D, DOM');
},
bullet: function(parameters) {
// do the stuff you were doing in your init function
return this;
}
});
Then your shoot function looks like this:
shoot: function() {
Crafty.e('bullet').attr({x: this._x+someMath, y: this._y+someMath})
.bullet(this._rotation);
}
I only rarely use the init function for anything other than setting up the required components.
Thats the kind of semantics Im trying to avoid though. It doesn't seem unreasonable that we should be able to pass parameters to the constructor/init method - that is a VERY common thing to want to do after all.
Maybe I'll look into offering a library patch...
@gregtillbrook There's a catch here: when you create an entity, you generally create multiple components at a time. Do each of those components init functions get the same arguments? Then you'd end up passing one huge mega-object when you create more complicated entities. I question whether
Crafty.e("ColorBullet", {
red: 20,
green: 50,
blue: 100,
x: this._x + dx,
y: this._y + dy,
rotation: this._rotation
})
is really better than
Crafty.e("ColorBullet")
.color(20, 50, 100)
.bullet(this._x + dx, this._y + dy, this._rotation)
Also, right now there's no guarantee when components are added to an entity that the init function is called -- if the components are already present on the entity, nothing happens. Either the user has to then keep careful track of the dependencies of various entities, or we have to recreate a component each time it's added, which has its own problems. (We used to do this, and it was the source of a few bugs.)
And finally, there's another issue: many components actually define setters for their properties in init. If, say, your init parameters were simply added to the entity before any components, that would mean they're added before the setters are declared, which I think would probably cause some weird issues.
Other than the name, I don't see your "afterInit" method as a kludge at all! :) That's exactly how many of Crafty's core components work; e.g. the "Color" component requires a call to .color(). Its init() method just sets up the necessary event bindings.
I'm definitely open to counter arguments, but right now I'd be opposed to adding this type of functionality.
P.S. An alternative in this particular case is just binding to the "Rotate" event:
this.bind("Rotate", function(e){ this.xspeed = this.speed * e.cos; this.yspeed = this.speed * e.sin});
You can do something similar any time you care about a property that might change later.
Hmm, fair enough - those are all good points (many I wasn't aware of). This is my first time using this pattern and as much as I like the composition it allows, stuff like the lack of init params is pretty odd to me.
I do still think init params would be useful in some cases - in fact, another component based library darlingjs (part inspired by crafty no less!) does cater for it - see https://github.com/darlingjs/darlingjs#create-entity
Granted, the gotchas you raise are all valid ones, but aren't they dependent on how you use crafty? e.g. I tend to init an entity once and rarely modify its components + Im unlikely to pass more than 3 params (x, y & a blob of config data) so the cons you mention are less of an issue for me.
We could expand the entity with an init object, this way you would have access to the properties and not big object would be passed around?
Ive long since moved on to other things so will close this one down.