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 momander

An entity's draw() method is called both during design-time (Weltmeister) and during run-time (when the game is played). Some custom drawing that works well during a game may not make sense or even be possible during design-time. For example, hitting ig.game.getEntitiesByType() won't work.

How can the code in my draw() method tell the difference between design-time and run-time?

This doc says that ig.game will be null during design-time:
http://impactjs.com/documentation/class-reference/ig-core#ig-game

So I wrote this code in my entity:

  draw: function() {
    this.parent();
    if (ig.game != null) {
      // Custom drawing code here.
    }
  }

But ig.game is not null (the debugger tells me it's an [object Object]) so the custom drawing logic runs, and I get errors. How can I avoid executing the custom drawing code during design-time?

1 decade ago by Arantor

Better still, during its definition, try adding a variable to it called debug, whose value is !!wm.Weltmeister - which will be true if in the editor and false if not.

Then you can just test that boolean - during the draw phase (or indeed anything else) you want to be doing absolutely as little as possible.

1 decade ago by momander

That sounds like a great solution! Various debug flags could be useful for other reasons as well.

From where can I examine wm.Weltmeister? I put code for it in my game's init() but got the error message that wm wasn't defined. I checked the docs but didn't find any reference to it there.

1 decade ago by Arantor

It should be a global variable... Actually just test for !!wm rather than anything else.

1 decade ago by momander

Thanks for the pointer! As wm is not defined during run-time, I got an error when I checked it during run-time. Here is what I ended up doing:

EntityFoo = ig.Entity.extend({

  init: function(x, y, settings) {
    try {
      this.DEBUG = !!wm;
    }
    catch (err) {
      this.DEBUG = false;
    }
  }

  draw: function() {
    this.parent();
    if (!this.DEBUG) {
      // Custom drawing code.
    }
  }
}

I still don't have the hang of Javascript idioms, so I don't know if this considered elegant or cludgy. But it works! Let me know if there is a better way.

1 decade ago by momander

Oh, if you cope and paste the code above, also include

this.parent(x, y, settings);

in the init() method. I forgot to paste that into my previous post.
Page 1 of 1
« first « previous next › last »