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?
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.
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.
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.
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.
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