Yes, I think it is a good way to listen attributes change.
this.on('attr:text', ...) // the script attribute `text` change
this.entity.on('attr:text', ...) // as entity don't have `text` attribute, this should not work
this.entity.element.on('attr:text', ...) // the element component attribute `text` change
There is a way to get this notification?
I’m building a set of script around the text element, and I can’t code them in a nice way because of this.
I had to expose another text attribute in my script, and add a setText() method so that I can do something once the text changes.
But having multiple scripts doing individual effects (shadow, scaling background, auto size of the font, etc…) is a problem. I had to create a main script just to set the text that will raise a custom event, so that my other scripts can be notified. It works, but if I can avoid it and use the element.text property itself would be nicer.
There is no event fired when the text of a text element changes at the moment. This is on purpose at the moment because firing events is not cheap and you can imagine having text elements changing text every frame like scores / timers etc…
It sounds like what you did with a custom setText method is a good solution, otherwise like @yaustar said you could maybe monkey patch the text property although that’s not recommended…
Thank you both. I’ll go with my custom text script then.
I didn’t know that firing not registered event could be a problem. I thought that you have some sort of array of function pointers to cycle, but if the array is null should be just the cost of an if to skip the whole event firing process.
I’ll take a look to the source code to see how is implemented.