Something seems to be quite wrong with 'Tag' (and app.fire-example)

Why can’t I do this:

var EventEmitterDip = pc.createScript('EventEmitterDip');

EventEmitterDip.attributes.add('once1200', {
    type: 'boolean',
    default: false
});
EventEmitterDip.attributes.add('innerIso', {
    type: 'number',
    array: true
});

// initialize code called once per entity
EventEmitterDip.prototype.initialize = function() {
    this.innerIso = this.app.root.findByTag("inner");
    console.log(this.innerIso.length);

    var app = this.app;

    setInterval(function() {
        app.fire("InnerOn");
    }, 1200);   

    app.on("InnerOn", function() {
        if (this.once1200===false) {
            this.inOn();
            this.once1200 = true;
        }
    }, this);
};

EventEmitterDip.prototype.inOn = function(dt) {
    this.innerIso.forEach(function(i) {
        this.innerIso[i].enabled = true;
    }); 
};
  • it returns:

Uncaught TypeError: Cannot read property 'null' of undefined

{ps: prior to that I also got errors in conjunction to your app.fire-example part (var app = this.app; will trigger another type of reference that often makes a conflict … a bit like when some of your examples use the ‘var self = this.app;’ structure )}

But main issue is the top one - I do get the length correctly at the console.log(this.innerIso.length);, so why not at separate class {inOn()}?
Seems very related to the inner workings of the compiler, where the setInterval-structures possess another scope (being loaded to early?)

I suspect the problem is here:

EventEmitterDip.prototype.inOn = function(dt) {
    this.innerIso.forEach(function(i) {
        this.innerIso[i].enabled = true;
    }); 
};

this will not be what you think it is inside the forEach function. forEach takes a second parameter that lets you control the value of this. So you probably want:

EventEmitterDip.prototype.inOn = function(dt) {
    this.innerIso.forEach(function(i) {
        this.innerIso[i].enabled = true;
    }, this); 
};

For more details on forEach check out: