Crafty icon indicating copy to clipboard operation
Crafty copied to clipboard

Init parameters for entities?

Open gregtillbrook opened this issue 12 years ago • 6 comments

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

gregtillbrook avatar May 22 '14 15:05 gregtillbrook

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.

samson212 avatar May 22 '14 20:05 samson212

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 avatar May 26 '14 15:05 gregtillbrook

@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.

starwed avatar May 26 '14 17:05 starwed

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.

gregtillbrook avatar Jun 04 '14 19:06 gregtillbrook

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?

kevinsimper avatar Jul 29 '14 23:07 kevinsimper

Ive long since moved on to other things so will close this one down.

gregtillbrook avatar Jul 27 '15 09:07 gregtillbrook