Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[API] Changes to attrs doesn't notify other scripts?

1593321345
Magnar S.
Pro
API Scripter
Hi! My script is changing the hp attr. I am also using DeathTracker, which adds a "bloodied" status marker below half health, which it does by watching for changes to the hp attr. When my script updates the hp attr, it seems that DeathTracker is not notified, because nothing changes. I've tried using setWithWorker, but that didn't help. What am I doing wrong? Thanks for any help.
1593322025
The Aaron
Roll20 Production Team
API Scripter
The API does not receive events for changes made by the API. To get that working, you have to set something up manually. Many scripts use the Observer pattern. You can look at Bump and GroupInitiative for an example. 
1593323500
Magnar S.
Pro
API Scripter
Thank you, that makes some sense. I see that the DeathTracker script registers a handler for TokenMod. I guess I'll try piggybacking on that, instead of having yet another observer that people need to include in their scripts.
1593324181

Edited 1593325002
Magnar S.
Pro
API Scripter
On that note, since TokenMod is such a widely used script, maybe it could expose ` notifyObservers ` on the `TokenMod` global name for this use case? Become the hub for API-to-API token change events, in some regard?
1593326411
Magnar S.
Pro
API Scripter
This seems to be working, but it would certainly be better if there was some centralised support for these things, either in the API, or from the community in some way. :) setTimeout(function () { if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange) { var original = TokenMod.ObserveTokenChange; TokenMod.ObserveTokenChange = function (handler) { original(handler); observeTokenChange(handler); }; log('Piggiebacking on TokenMod for token change observations.'); }; }, 1);
1593354754

Edited 1593363925
The Aaron
Roll20 Production Team
API Scripter
I definitely agree that a centralized notification system for API changes would be a good thing to have. TokenMod isn't the right place for it, but it could certainly be a client of it. The complicated part (and the reason it isn't built in already) is the need to avoid cyclic notifications. Similarly problematic is multiple asynchronous operations during the handling of an event. It can get complicated pretty quickly. 
1593355617
Magnar S.
Pro
API Scripter
Yeah, I see the problem. However, since API scripts normally do not trigger each other, the central system would be the able to stop infinite recursion. Maybe the triggering event could include a UUID, and the notification system would stop a single handler from being called more than once per ID? 
1593356094
Magnar S.
Pro
API Scripter
And while you say that TokenMod is not the place for it (a notion I can certainly agree with from a software engineering standpoint), it seems like my piggieback-technique chose the right integration point in TokenMod, since it is supported by quite a few libraries: InspirationTracker MapLock TokenLock DeathTracker LazyExperience StatusInfo TokenCondition Bump CombatTracker CombatMaster