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

set does not trigger events?

1384578241

Edited 1384578677
I have a script that listens to on("change:token", function(obj, prev) and it triggers as expected when I manually change the values of a bar/circle using the UI. So far, so good. I also wrote a 2nd script in order to give damage to a group of selected tokens at once. (because I can't figure out how to do it in the UI...) It uses obj.set("bar1_value", Damage ); and I call it from the IU using the "!" prefix and it also works well. HOWEVER!!!! For some reason, the second script does not trigger the first! It is modifying the token's attribute, and the event listener doesn't hear it. What am I missing?
1384580301
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
I would set up a function.... var MyCode = MyCode || {}; MyCode.MyProcess = function MyProcess(MyVar, MyOtherVar) { //Whatever you want to do }); on("change:token", function(obj, prev) { //Call whatever you want to do MyCode.MyProcess("X","Y"); }); //Whenever you do: obj.set("bar1_value", Damage ) //Follow with: MyCode.MyProcess("X","Y"); On "Change:token" call MyCode.MyProcess("X","Y"); Or whenever you want to "set" "bar1_value" and follow with a like action to on "Change:toke" call MyCode.MyProcess("X","Y");
1384580672

Edited 1384580691
I'm correct then? Listener events only trigger through use of the UI? That is completely unlike what I'm used to. So, I would be able to make this function in a 3rd script, then, yes? Like a supporting file? (DLL or SO)
1384583280
Lithl
Pro
Sheet Author
API Scripter
Indeed, this was a surprise to me when I learned it just a month or so ago. Particularly since sendChat will trigger chat:message events!
1384607032
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Brian said: Indeed, this was a surprise to me when I learned it just a month or so ago. Particularly since sendChat will trigger chat:message events! Does sendChat trigger the chat event? Isn't the message output of the chat triggering the on("chat:message") event? When you sendChat for a dice roll wanting the result just for the API there is no message and therefore no on("chat:message") triggered. Rgiht?
1384607757

Edited 1384607868
Riley D.
Roll20 Team
This was intentional, to avoid causing infinite loops. Basically my concern was that people would do something like: on("change:token", function(obj) { obj.set({70 + obj.get("left")}); }); ...doing that would create an infinite loop if the events were triggered by local API changes. And how would you be able to tell if you were responding to an event from a player, or an event from the API? (You'd have to store the previous value in state or something and check it). So basically just think of the events as "remote" changes, and then if you are doing something inside an API script and want to do something else as a result, you manage that yourself by calling additional functions.
Brian said: Indeed, this was a surprise to me when I learned it just a month or so ago. Particularly since sendChat will trigger chat:message events! Yeah, sendChat() and chat events are just a whole different beast, because they aren't "object events."
Thank you everyone for your prompt replies. Especially Stephen S. for a solution, and Riley D. for an explanation!
1384618532

Edited 1384618664
Ok, I have tried several permutations of the sample code given above, and I get the same result for each one. Unexpected token ) I've tried... --THIS-- var L47FC = L47FC || {}; L47FC.StdChrDmgSav = function(obj) { //Whatever you want to do }; --AND THIS--- var L47FC = L47FC || {}; L47FC.StdChrDmgSav = function StdChrDmgSav(obj) { //Whatever you want to do }; . . . and many more. Funny thing is, there's only 1 ) in the entire code. In the sample code given above, there's an extraneous one at the end of the function declaration which I removed. I cannot get these few lines to be accepted by the API editor. Anyone?
1384619283
Lithl
Pro
Sheet Author
API Scripter
Stephen S. said: Brian said: Indeed, this was a surprise to me when I learned it just a month or so ago. Particularly since sendChat will trigger chat:message events! Does sendChat trigger the chat event? Isn't the message output of the chat triggering the on("chat:message") event? When you sendChat for a dice roll wanting the result just for the API there is no message and therefore no on("chat:message") triggered. Rgiht? That's correct if you include a third argument to the sendChat function. If you only use two arguments, the result will trigger a chat:message event.
Lifer4700 said: Ok, I have tried several permutations of the sample code given above, and I get the same result for each one. Unexpected token ) Well, I fixed it. I'm not sure how, but everything works perfectly. I copied all my scripts into Notepad++, deleted them from my sandbox, then re-created them all pasting everything back in from Notepad++.