When extending Scene which "engine" to use in lifecycle methods? Instance member or passed arg?

40 views
Skip to first unread message

Ben Lakey

unread,
Nov 11, 2018, 4:57:22 PM11/11/18
to excaliburjs
When you extend "Scene" and implement things like "onInitialize" you have a decision to make:  You can either use the "engine" that was passed to "onInitialize" as an argument, or you can use "this.engine" which is available on "Scene" as an instance member.  Which should I use? Why are both available?

export default class Splash extends Scene {
 
public onInitialize(engine: Engine) {
   
// "engine" from the arguments above?
   
// or
   
// "this.engine" available on this instance?
 
}
}

Thanks!

Erik Onarheim

unread,
Nov 24, 2018, 9:35:54 AM11/24/18
to excaliburjs
Hi Ben,

Apologies on the late reply!

I recommend using the onInitialize to get at an instance of engine, onInitialize is guaranteed to be called with engine when initialized by Excalibur. Additionally, I recommend putting any initiailzation logic in onInitiailze on any Excalibur entity that has an onInitialize.

Referencing this.engine in ex.Scene can be null if it has not yet been added to an engine. Which could be an issue depending on how your game builds, initiailizes, and adds scenes. In fact we probably should make it a private or protected member to avoid confusion and steer folks towards onInitialize as the best way to get at a reference to engine.


Let me know if there are more questions,
Erik
Reply all
Reply to author
Forward
0 new messages