1 decade ago
by ape
What&
039;s the best way to ensure that #ig.game
is available in entities that are spawned within
game.main
?
For example, let&
039;s say I have an entity I use for grouping "child" entities. I spawn my "parent" within the #init
method in
game.main
using
this.spawnEntity()
.
I then want the "parent" to take care of spawning its children. So I find myself doing this a lot:
update: function() {
this.parent();
if (ig.game && this.loaded == false) {
this.loaded = true;
ig.game.spawnEntity(Foo,0,0);
}
},
It works fine, but it doesn&
039;t feel right. I kinda wish I could just rely on #ig.game
being available.
Thoughts?
1 decade ago
by MikeL
Funny you should mention that. We were just talking about that on IRC earlier. I think that Dominic is going to come up with a more formalized way of doing this. There were some suggestions made.
It appears that in your main.js init, you can do this:
ig.game = this;
In your case, before you spawn anything. Again, I think that Dominic will be making a more formal way of doing this.
1 decade ago
by dominic
ig.system
creates your game class like this:
ig.game = new (gameClass)();
So first the
init()
method of your game class is called and only after it has completely finished, the instance is assigned to
ig.game
. Use the workaround from MikeL if you want to use
ig.game
during your game&
039;s #init()
.
I think I will put an
init()
method into the base
ig.Game()
class for the next version. It then assigns itself to
ig.game
- requiring you to call
this.super()
in your game&
039;s #init()
seems like a cleaner solution than setting
ig.game
yourself. Still not perfect though :/
1 decade ago
by Jesse
That could almost be worse for someone that doesn't call the parent (the implementation of "init" for the Game class) because ig.game will never be defined! You could also leave in what you currently have as a back-up.
A fool proof way to solve it at the engine-level is to define staticInstantiate in the Game class and set it there. I just tried it and saw it go from undefined to an instance of my subclassed Game.
staticInstantiate: function() {
ig.game = this;
}
Now I can use "ig.game" from within any Entity, whereas before it was throwing a null reference exception for at least one "update" call of one of my entities. Because I was loading a level inside of the "init" of my game, ig.game wasn't set yet...but now it is.
1 decade ago
by ape
@MikeL: Thanks! that's so embarrassingly simple.
@Jesse: You bring up a good point. Though I wonder how often people are subclassing Game, let alone subclassing it without calling this.parent();
I think for now I&039;ll go with MikeL's solution so it's both simple and explicit. In fact, I'd be comfortable having it work that from now on. It's far more explicit than calling #this.super()
as dominic suggested for future releases.
Page 1 of 1
« first
« previous
next ›
last »