Is there an event fired for when an entity is enabled/disabled

I would like a script to be able to listen out for when its own entity has been either enabled or disabled.

This API page only lists a destroy event for entity:

However this forum post seems to suggest there is in fact such an event: "enable" event doesn't trigger after initialization

I’ve tried:

this.entity.on('enable', ...)

and

this.entity.on('enabled', ...)

…but to no avail. Am I missing something? (sorry if it’s obvious)

The event doesn’t trigger when an entity is created or when the scene loads but only when you disable it and enable it again

Yeah, I read that in the above post, but I can’t seem to get it triggering even when it’s enabled later on having already been disabled. The event is just called ‘enable’ right? and I attach it to the entity?

1 Like

There is an event that is fired (surprised it isn’t in the documentation). However, if an entity starts off disabled, the initialize function of the scripts attached are not called. It gets called on the first time the pc.script gets enabled.

So the way around this is to call the callback in the initialize function and also subscribe to the event.

App.prototype.initialize = function() {        
    this.onEnable();
    this.entity.on('enable', this.onEnable, this);
};

App.prototype.onEnable = function() {        

};

Edit: I actually can’t see in the code base where the entity fires the event but the script component does fire a ‘state’ event.

App.prototype.initialize = function() {        
    this.onEnable();
    this.on('state', this.onStateChanged, this);
};

App.prototype.onStateChanged = function(state) {        

};
1 Like

Thanks yaustar. Couldn’t get the enable event to work, but the state change event seems to work fine, so I’ll go with that for now. Thank you.

In this case, if an entity is starts enabled, wouldn’t that result in the onEnable() method being called twice?
After all, there may be subscriptions to events, etc. And that’s not good.

The initialize function of script is called after the enable event on the Entity so it will not be called twice. And until the initialize function of the script is called, there is no subscription to events.

1 Like

What are the possible state values?

Check here:

https://developer.playcanvas.com/en/user-manual/scripting/anatomy/#state,-enable,-disable

1 Like

this didn’t work for me :frowning:

Subscribing to ‘enable’ and ‘state’ on the Entity didn’t work for me either, but subscribing to the event on the ScriptType object itself does:

    this.on('enable', this.onEnable, this);

This is a bit of a necro, so I’m not sure if this broke or changed intentionally in the last two years, but the documentation still says call it on the entity and this is the only relevant forum post, so hopefully this helps anyone trying to figure this out.

Do you have a link to the doc please?

I was talking about this page, though I think now I misread it as saying you subscribe to the event on the Entity, but its saying that if you disable the Entity it will fire the event on the Script. The code sample looks correct in the doc.

Is there a plan to add Script methods for onEnable, etc. like initialize()? It would help to make the API self documenting so we don’t have to rely on googling for forum posts and docs.

I don’t think so. Much of the engine is driven by the event system so I doubt extra functions will be added for all the events you can get on the components and entity.

At best, maybe the script default template can add some of the more common events automatically?

What would that look like? Wouldn’t it add runtime overhead to the default template?

If script methods aren’t on the table, the existing static variables enumerating the event names (i.e. Script.EVENT_ENABLE) work for self-documentation as well, they’re just not quite as convenient or easy to find. Though I did notice the engine code doesn’t actually use them when firing the events, so right now its pretty brittle as well.

It be something like this

MouseInput.prototype.initialize = function() {        
    this.on(pc.ScriptComponent.EVENT_DESTROY, this.onDestroy, this);
};

MouseInput.prototype.onDestroy = function() {
}

Users can just delete what they don’t need. There was a request a while back for people to provide their own templates but that’s been on the shelf for a while.

The events in the public API should never break. It’s will only be brittle for those working directly in the engine itself but the tests in the release process and automated should catch those breakages.

The entity enable event from 2018 above was private API event hence it’s not available now

the tests in the release process and automated should catch those breakages.

Nice, in that case the static EVENT_ variables work fine for me.

Users can just delete what they don’t need.

They can, but seems like every codebase I work on folks just leave all the empty templated methods. Keeping the template minimal is better IMO.