`unregisterBeforePhysicsStep` doesn't work with binded functions

I register method with a bind feature:

playerImpostor.registerBeforePhysicsStep( this.physicsUpdate.bind( this ) );

I tried to unregister it with:

playerImpostor.unregisterBeforePhysicsStep( this.physicsUpdate.bind( this ) );
playerImpostor.unregisterBeforePhysicsStep( this.physicsUpdate );

In both cases I get:

BJS - [17:54:25]: Function to remove was not found

It works in one case only if I register without using bind feature:

playerImpostor.registerBeforePhysicsStep( this.physicsUpdate );

Then it unregister it without any problem. So that is not comfortable to use in some cases because I can’t call method from my context.

Is that a bug or a normal behaviour?

Normal behavior. the bind function returns a new function every time it is executed. You will have the same behavior with the browser’s event listeners as well.

Just bind to a variable and pass the variable to the register and unregister functions and you are good to go.

3 Likes