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

[HELP] Script Involving Object Detection and Macro Calls

As of late I've been working on having enterable buildings (ones that personalize so they are only visible to those inside of them). To do this I've worked with a teleporter script to control token movement once a player reaches the front door and I've gathered what I need to adjust what layer the roof object and the walls (for dynamic lighting) are on. My final hurtle lies with a script that can detect that token movement and call a macro. Example: Player 1 walks to door 1. Door 1 has a token on the gm layer labled Door_A_01 that is linked to another token on the gm layer labeled Door_B_01. When Player 1's token is on the same grid square as Door_A_01 he is moved to Door_B_01 and the chat receives the callout #Door1. The macro #Door1 calls upon another script to move the roof of Building 1 to the gm layer and move the walls from the gm layer to the dynamic lighting layer. When Player 1 steps back on Door_B_01 the state reverts to the previous position.
1537489359
The Aaron
Pro
API Scripter
So, you're asking how you would: 1) Detect that a token has been moved on('change:graphic', (obj,prev) => { /* stuff */ }); 2) Determine what it overlaps. const findContains = (obj,filter,layer) => { let cx = obj.get('left'), cy = obj.get('top'); filter = filter || (() => true); if(obj) { layer = layer || 'gmlayer'; return findObjs({ _pageid: obj.get('pageid'), _type: "graphic", layer: layer }) .filter(filter) .reduce((m,o) => { let l=o.get('left'), t=o.get('top'), w=o.get('width'), h=o.get('height'), ol=l-(w/2), or=l+(w/2), ot=t-(h/2), ob=t+(h/2); if( ol <= cx && cx <= or && ot <= cy && cy <= ob ){ m.push(o); } return m; },[]); } return []; }; So, putting that together, something like: const findContains = (obj,filter,layer) => { let cx = obj.get('left'), cy = obj.get('top'); filter = filter || (() => true); if(obj) { layer = layer || 'gmlayer'; return findObjs({ _pageid: obj.get('pageid'), _type: "graphic", layer: layer }) .filter(filter) .reduce((m,o) => { let l=o.get('left'), t=o.get('top'), w=o.get('width'), h=o.get('height'), ol=l-(w/2), or=l+(w/2), ot=t-(h/2), ob=t+(h/2); if( ol <= cx && cx <= or && ot <= cy && cy <= ob ){ m.push(o); } return m; },[]); } return []; }; on('change:graphic', (obj,prev) => { // check if obj is the right one let c = getObj('character',obj.get('represents')); if(c && c.get('controlledby')) { let trigger = findContains( obj, (o) => /^door_/i.test(o.get('name')) ); if(trigger.length){ let name = trigger[0].get('name').match(/^door_([^_]+)_(\d+)$/i); sendChat('',`/w gm #Door${parseInt(name[2])}`); } } }); Don't know if that will work for what you're doing, but that should give you something to poke at...
Now the last line there sendChat('',`/w gm #Door${parseInt(name[2])}`); What name is that generating?
1537495971
The Aaron
Pro
API Scripter
In Javascript ES6 and greater, ` ` denotes a Template Literal , in which ${} gets substituted for the contents after evaluating them.  parseInt() will turn whatever is in name[2] into an integer (it's a string initially).  name[2] in this case is 01 for Door_A_01.  So, it should end up looking like: sendChat('','/w gm #Door1'); It would be easier if the macro name's number matched then number in the token name.
The Aaron said: In Javascript ES6 and greater, ` ` denotes a Template Literal , in which ${} gets substituted for the contents after evaluating them.  parseInt() will turn whatever is in name[2] into an integer (it's a string initially).  name[2] in this case is 01 for Door_A_01.  So, it should end up looking like: sendChat('','/w gm #Door1'); It would be easier if the macro name's number matched then number in the token name. Thankfully I can rename the Doors to almost whatever I want as they use A-L to determine the number of teleports in the sequence and the 01 dictates the set.