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 Zetaphor

Hello everyone, I've got a small hangup that's stopping me from progressing, I appreciate any help you can give.

In short, I'm attempting to read ig.game.entities from a class which is instantiated as an object within an entity. The problem is ig.game is returning null.

The class is stored in a separate file in a subfolder under entities. I'm requiring it within my entity file. I can see that it is executing, but it fails when I attempt to reference the entities list for iteration.

ig.module(
	'game.entities.components.target'
)
.requires(
	'impact.game'
)
.defines(function(){

ig.component_target = ig.Class.extend({
    findCreature: function(self,options,distance){
        console.log(MyGame);
        var results = new Array();
        for(var i = 0; i < ig.game.entities.length; i++) {
            var ent = this.entities[i];
            for(o_property in options){
                if (ent.properties[o_property] == self.properties[o_property]){
                    results.push(ent);
                }
            }
            if (!results.length){
                return false;
            }
            else{
                return results;
            }
        }        
    }    
});
});

1 decade ago by Zetaphor

Here's my use of the class under the entity

.requires(
	'impact.entity',
        
        /*COMPONENTS*/
        'game.entities.components.target'

....

	size: {x:16, y:16},
	collides: ig.Entity.COLLIDES.ACTIVE,
        
        /*COMPONENTS*/
        component_target: new ig.component_target(),
        /*END COMPONENTS*/

....
                //This is under init
                this.component_target.findCreature(this,options,0);                

1 decade ago by Zetaphor

Does nobody have any ideas on this one? I feel like I'm missing something obvious here...

1 decade ago by samgreen

Change this line:
var ent = this.entities[i];

to this:
var ent = ig.game.entities[i];

1 decade ago by dominic

We just figured it out in IRC. The problem is actually that ig.game is only set after the Game's init() method has returned. More info in this thread.

To fix it, insert this line at the beginning of the Game's init():
ig.game = this;

This will be fixed in the next version. Sorry for the trouble!

1 decade ago by Zetaphor

The above fix did solve my problem of ig.game.entities returning null, but now it seems that the entities list never gets populated.
ig.game.entities.length = 0!

1 decade ago by dominic

If you access ig.game.entities while the level is still being loaded, it only has those entities that are already spawned. I.e. the init() method on your entities is called as soon as the entity is created. If it's the first entity to be created, the entities array will still be empty.

You could provide another method for entities that get's called once the level is completely loaded. Overwrite loadLevel() to do so:

loadLevel: function( data ) {
	// load the level
	this.parent();
	
	// go through all entities and call 'afterLevelLoad' on each
	for( var i = 0; i < this.entities.length; i++ ) {
		
		// does the entity have an 'afterLevelLoad' method?
		if( this.entities[i].afterLevelLoad ) {
			this.entities[i].afterLevelLoad(); // call it!
		}
	}
}
Page 1 of 1
« first « previous next › last »