I think it would be convenient to have all of the functions declared within a GameObject be methods of the GameObject. Currently, the start and update functions are actually methods of the GameObject. However, all other functions are methods of the Window. Here is a simple example demonstrating the current functionality:
// It would be nice to have functions become methods of the// object. I am using a self = this hack and adding member// variables which also is slightly broken.// called once when the game startsfunctionstart(){this.hp=8;}// called every framefunctionupdate(){// This is a method of the GameObjectif(isKey('q'))say(`I have ${this.hp} hp`);if(isKey('w'))notAMethod();if(isKey('e'))pythonStyleMethod(this);}functionnotAMethod(){// `this` is the Window objectsay(`I have ${this.hp} hp`);}functionpythonStyleMethod(self){say(`I have ${self.hp} hp`);}It isn't completely necessary to add this as there are work arounds. For example, in the start method you can declare a method and forward it to a function. At that point, the this keyword will work in the context of update. BUT it will not work in the context of any other methods. In the example below, there is a workaround to declare a GameObject variable self which can be used throughout all of the functions. It feels a little bit hacky but could potentially be the way Code train handles "public / private" methods and fields.
letself;functionstart(){this.hp=8;this.addMethod=notAMethod;self=this;}functionnotAMethod(){// `this` is the Window objectsay(`I have ${this.hp} hp`);}
I think it would be convenient to have all of the functions declared within a GameObject be methods of the GameObject. Currently, the
startandupdatefunctions are actually methods of the GameObject. However, all other functions are methods of theWindow. Here is a simple example demonstrating the current functionality:It isn't completely necessary to add this as there are work arounds. For example, in the
startmethod you can declare a method and forward it to a function. At that point, thethiskeyword will work in the context ofupdate. BUT it will not work in the context of any other methods. In the example below, there is a workaround to declare a GameObject variableselfwhich can be used throughout all of the functions. It feels a little bit hacky but could potentially be the way Code train handles "public / private" methods and fields.