Event is the only script communication method in script 2.0?

I find Event and the Communication page in script manual has merged ~

And when using code like


the function cannot access instance local data like

// initialize code called once per entity
player.prototype.initialize = function() {
    // local rotation or world rotation
    this.local = false;

player.prototype.jump = function(){
       consle.log(this.local);   //undefined when calling by playerEntity.script.player.jump();

So, if Event is the only way. Is there any pattern can be reference ?

I’m currently using Events like:

this.app.fire(this.entity.name + ':player:jump');

Is this a good practice ?

It might be undefined because you have swap method on player script and you have resaved your script and you did not inherited local field from old script.

Just made test project and it works perfectly, just try: https://playcanvas.com/editor/scene/446676
As well bear in mind that initialize is not called when entity is created if it is or its any ancestor are disabled, it only get called when it’s first time about to update (get enabled).

Thanks max for sharing the project, this was of great help. :slight_smile:

1 Like