You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WIP: Undefined system instance members if TypeScript has useDefineForClassFields activated and is transpiled with @babel/plugin-proposal-class-properties
#50
Open
martinemmert opened this issue
Oct 27, 2020
· 1 comment
tl;dr; The reason why this behavior is occurring is currently not known and I still investigate the issue.
It can be resolved by deactivating the useDefineForClassFields compiler option of Typescript.
Both this.view and this.displayObjectQuery are undefined when accessing them inside the update method and are undefined if the code is paused by the debugger. At the end of init both are defined.
I need to have a better understanding of the System class to go on with my investigations, but if by chance anyone else has an idea - I would be glad if we can resolve this.
Note: if placed some comments into the code as hints
import{Query,System,World}from'ape-ecs';importtype{View}from'../components/View';exportclassRenderSystemextendsSystem{publicview?: View;publicdisplayObjectQuery!: Query;init(){this.view=this.world.getEntity('RenderView');this.displayObjectQuery=this.createQuery().fromAll('DisplayObject').persist();console.log(this.view)// output of the RenderView Entity object}update(){if(!this.view||!this.view.stage||!this.view.renderer)return;// is never executed beacue this.view is undefinedthis.displayObjectQuery.execute();this.view.renderer.render(this.view.stage);}}
Typescript compiler compiles that to:
import{System}from'ape-ecs';exportclassRenderSystemextendsSystem{view;displayObjectQuery;init(){this.view=this.world.getEntity('RenderView');this.displayObjectQuery=this.createQuery().fromAll('DisplayObject').persist();console.log(this.view);// output of the RenderView Entity object}update(){if(!this.view||!this.view.stage||!this.view.renderer)return;// is never executed beacue this.view is undefinedthis.displayObjectQuery.execute();this.view.renderer.render(this.view.stage);}}
Babel transpiles that to:
function_defineProperty(obj,key,value){if(keyinobj){Object.defineProperty(obj,key,{value: value,enumerable: true,configurable: true,writable: true});}else{obj[key]=value;}returnobj;}import{System}from'ape-ecs';exportclassRenderSystemextendsSystem{constructor(...args){super(...args);// if I remove this by hand after transpiling, this.view is defined during update_defineProperty(this,"view",void0);// if I remove this by hand after transpiling, this.displayObjectQuery is defined during update_defineProperty(this,"displayObjectQuery",void0);}init(){this.view=this.world.getEntity('RenderView');this.displayObjectQuery=this.createQuery().fromAll('DisplayObject').persist();console.log(this.view);// output of the RenderView Entity object}update(){if(!this.view||!this.view.stage||!this.view.renderer)return;// is never executed beacue this.view is undefinedthis.displayObjectQuery.execute();this.view.renderer.render(this.view.stage);}}
The text was updated successfully, but these errors were encountered:
I'm experiencing the same issue running Ape ECS in a Next.js project using the babel/next preset which includes @babel/plugin-proposal-class-properties.
Unfortunately, your workaround with disabling useDefineForClassFields is not working for me (it should be false by default anyway), so I'm pretty stuck.
Update:
Adding @babel/preset-typescript seems to fix the missing queries.
tl;dr; The reason why this behavior is occurring is currently not known and I still investigate the issue.
It can be resolved by deactivating the
useDefineForClassFields
compiler option of Typescript.Both
this.view
andthis.displayObjectQuery
are undefined when accessing them inside the update method and are undefined if the code is paused by the debugger. At the end ofinit
both are defined.I need to have a better understanding of the
System
class to go on with my investigations, but if by chance anyone else has an idea - I would be glad if we can resolve this.Note: if placed some comments into the code as hints
Typescript compiler compiles that to:
Babel transpiles that to:
The text was updated successfully, but these errors were encountered: