Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Nozferatu

In fundamental OO we know that we use properties/attributes to describe an object.

So what would be the best practice for handling values used strictly for processing that don't really describe our object - but need to be kept beyond a single iteration of our update loop?

To give an example, what would be a good way of handling a timer that causes our player to do something approximately every 5 seconds?

EntityPlayer = ig.Entity.extend({

    timer: new ig.Timer(5),
    …

    update: function() {
        this.parent(); 
        if( Math.floor(this.timer.delta()) % 5 == 0 )
        {
            // do something every 5 seconds
        }
    },

    …

});

I feel really dirty using attributes for this sort of thing. This timer is only used for this single object instance, no other objects need to know about it so why is it an attribute? What's a better way?

1 decade ago by vincentpiel

1) If you define the timer object within the attributes, it will be shared amongst all instances, which in this case won't hurt, but still -->> define the properties that inherit from ig.Class for which you want one instance per entity instance inside the init(). (expl : ig.Animation).

2) do not hesitate to use specific properties : there's no such things as 'dirty' : is it slower / memory greedy / unmaintanable code ? are the only questions you should ask yourself.
Building a specific behaviour does require specific data.

3) you can make clearer that it is a temp variable by using conventions : _ prefix for private properties is quite a common convention, i use double _ for properties used only for optimisation purposes, and '_xxxx_' for system properties. Build your own conventions so that you can quickly identify the role of a property/method.

4) If you want, you can define some properties as non enumerable, so that they won't show in a
for (var p in myObject) {
}

to do that write, in the init() :
 Object.defineProperty(this, '_timer', { value : new ig.Timer(...) } );

(And just for the record, your logic is better written with :
        if  (this._repeatTimer.delta()>0) 
        {
            this._repeatTimer.set(this._repeatDelay);
            // do something every _repeatDelay seconds
        }

)

1 decade ago by vincentpiel

(double post, sorry, delete this one if possible)

1 decade ago by Nozferatu

Sounds good, just wanted to check in to see if there was a cleaner solution to this. Excellent tips, I definitely like the idea of name conventions.
Page 1 of 1
« first « previous next › last »