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

add:graphic is only triggered once using createObj

Hey there, I have 2 API Scripts one is creating a graphic, the other uses the event add:graphic . The first time after saving the script using add:graphic  it is triggerd properly. Afterwards it is not triggered anymore. Creating a graphic by dragging it in from the journal works fine. I tested it with one script. Same result. Here is my testscript: What am I doing wrong? var API_Meta = API_Meta || {}; //eslint-disable-line no-var var TestApi = TestApi || (function () { 'use strict'; //Update your API Command var apiCall = '!testapi'; var generateToken = function () { var characterId = '-N-C69c9o6i9kSTpH1tW'; var pageId = Campaign().get("playerpageid"); var character = getObj("character", characterId); const imgsrc = getCleanImgsrc(character.get('avatar')); createObj('graphic', { _type: "graphic", _subtype: "token", _pageid: pageId, represents: characterId, layer: "objects", isdrawing: false, imgsrc: imgsrc, top: 210, left: 210, width: 70, height: 70, }); } var testAddGraphic = function (obj) { log(obj); } var getCleanImgsrc = function (imgsrc) { var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/); if (parts) { return parts[1] + 'thumb' + parts[3] + (parts[4] ? parts[4] : `?${Math.round(Math.random() * 9999999)}`); } return; } //Event Handlers var handleInput = function (msg) { if (msg.type !== "api") { return; } if (!msg.content.startsWith(apiCall)) { return; } generateToken(); } var checkInstall = function () { } var registerEventHandlers = function () { on('chat:message', handleInput); on('add:graphic', testAddGraphic);; }; return { CheckInstall: checkInstall, RegisterEventHandlers: registerEventHandlers }; }()); on("ready", function () { 'use strict'; TestApi.CheckInstall(); TestApi.RegisterEventHandlers(); });
1687228945
timmaugh
Forum Champion
API Scripter
Hey, Kerubis... Actions taken by scripts don't emit events (with the exception of the chat event... maybe one more I can't remember at the moment). So if you have a script that is going to create a token, you might want to establish an Observer Pattern and let other scripts register so that they can know when your script creates a token. TokenMod and ChatSetAttr both do this. Basically, you'll want to let scripts register a function (a handler), and you'll need to keep an array of those functions so that when you create a token, you can pass an obj and a prev to them. Your registerObserver function will need to be exposed in your return {} statement so other scripts can use it. We can provide an example, if you need help.
Thank you, I did not know that events arent triggered, kinda weird that it is triggered once though. I am familiar with Observer Patterns. Gonna implement it that way.