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

Trigger Custom Event

1421977916

Edited 1421978161
Is there a way to trigger a custom event from a script that other listeners could react to? Something along the lines of: trigger('myevent:fired', arg1, arg2); // And then in some other script on('myevent:fired', function (arg1, arg2) { log(arg1); log(arg2); }); I can probably expose some sort of Observable interface to let other scripts register listeners, but I was just wondering if there was an existing way to link into the general event structure.
1421980318
The Aaron
Roll20 Production Team
API Scripter
You'll have to roll-your-own, as you suggested. There isn't an interface for this exposed to the API scripts.
Ok, good to know. Thanks for the info.
1422068440

Edited 1422068698
DXWarlock
Sheet Author
API Scripter
Unless Im missing the situation of why it would need its own event handler, couldn't you just make it a function? myevent(arg1, arg2); // And then in some other script function myevent(arg1, arg2) { log(arg1); log(arg2); }); As all scripts can see other scripts anyway. at least from what I'm assuming you want. Thats how I do mine I have one script called "Calls/Functions" with every one of my functions in it. so I can call it from any other script for frequently used, or common functions I need.
1422072897
Lithl
Pro
Sheet Author
API Scripter
William, Andrew is talking about making an observable interface. Basically, you do something along the lines of observe(myObject), and then whenever myObject is changed, the observable's callback runs.
1422077824

Edited 1422078546
The Aaron
Pro
API Scripter
Brian said: William, Andrew is talking about making an observable interface. Basically, you do something along the lines of observe(myObject), and then whenever myObject is changed, the observable's callback runs. The salient point being the object having the event (A) does not need to be aware of the objects that care about the event (Bs). With a function, the A must know that B exists (or at least know to check for it) and know it has a function HandleEvent(), and then call it. A depends on the existence of B. With the Observable Design Pattern, A is "Observable". It provides an interface like on(), whereby Bs ("Observers") can subscribe to an event (which is precisely what things like on('ready',func) do). A simply has a list of functions it has been given, associated with events it has. When A has an event, it calls each of the functions associated with that event. B then depends on the interface of A. This is important as it allows A to be decoupled from its users and provide its facilities in a general and useful form to many possible clients. <a href="https://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">https://en.wikipedia.org/wiki/Observer_pattern</a>