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

Request: Toggle grid snap

1538438181
Mik Holmes
Pro
Marketplace Creator
Had a request that seems easy, but I haven't found an example of it, and I'm not versed enough in javascript to do it myself. I'm looking for an API script to toggle the grid (or at least snapping) on/off, so I can make a quick macro for in/out of combat. While there's a few alternatives, they have a few problems in my game. I can instruct my players to use alt-dragging outside of combat, but I have at least one player playing on tablet and we haven't found a good workaround. I can also use token-mod to set isDrawing, but this removes the ability to edit condition markers via dropdown (though I can change them with a token-mod macro). I can toggle the grid manually in page settings, but it's difficult to do quickly mid-game; and besides I'd like to couple toggling the grid with a "in-combat" and "out-combat" macro containing other scripts.&nbsp; In&nbsp; <a href="https://wiki.roll20.net/API:Objects#Page" rel="nofollow">https://wiki.roll20.net/API:Objects#Page</a> &nbsp;I see there are showgrid and snapping_increment objects. I don't know if they could be set in scripts, but if they could, some sort of "!gridtoggle --on/off" would really help my game. Is this something easily doable? I wouldn't want anyone spending more than a couple minutes on this, but if it's trivial, I'd really appreciate it.
1538442732
GiGs
Pro
Sheet Author
API Scripter
I dont think the API has access to those settings unfortunately.
1538444224

Edited 1538455100
Bast L.
API Scripter
Edit: Oh, also it toggles between 1 and 0. Hmm. Could probably store a non-1 value in state for more generalized toggling. I tried just turning the grid off and on, but that only made it disappear, not remove snapping. This seems to work. I only tested it a little bit. Works only for gm, and only on page with player flag: //Toggles Grid on and off with a button. //Make macro and put in macro bar. Call it GridToggle, or whatever //Body of macro reads: !GridToggle on ( "ready" , function () { on ( 'chat:message' , function ( msg ) { if ( msg . type === "api" &amp;&amp; msg . content === "!GridToggle" &amp;&amp; playerIsGM ( msg . playerid )) { let campaign = findObjs ({ _type: "campaign" })[ 0 ]; let page = findObjs ({ _type: "page" , id: campaign . get ( 'playerpageid' )})[ 0 ]; if ( page . get ( "snapping_increment" ) == 1 ) { page . set ( "snapping_increment" , 0 ); } else { page . set ( "snapping_increment" , 1 ); } } }); });
1538444969
Mik Holmes
Pro
Marketplace Creator
Hey thanks Bast, that works perfect! I do typically use increment 1, though I'm running a marketplace module with a few pages that don't use 1 (though not for a while, so this is great for the meantime).
1538445253
GiGs
Pro
Sheet Author
API Scripter
nice, Bast! I didnt realise we had access to page settings.
Thanks. I think Aaron helped me with snapping_increment stuff when I was working on some script to turn those 10 foot square maps into 5 foot ones (though Robin has a much better version of that script). New version, allows for multiple snapping increments, slightly more annoying to use: //Toggles Grid on and off with a button. //Make macro and put in macro bar. Call it GridToggle, or whatever //Body of macro reads: !GridToggle ?{Snap Increment|1|0.5} on ( "ready" , function () { on ( 'chat:message' , function ( msg ) { if ( msg . type === "api" &amp;&amp; msg . content . split ( ' ' )[ 0 ] === "!GridToggle" &amp;&amp; playerIsGM ( msg . playerid )) { let snapInc = parseFloat ( msg . content . split ( ' ' )[ 1 ]); let campaign = findObjs ({ _type: "campaign" })[ 0 ]; let page = findObjs ({ _type: "page" , id: campaign . get ( 'playerpageid' )})[ 0 ]; if ( page . get ( "snapping_increment" ) == snapInc ) { page . set ( "snapping_increment" , 0 ); } else if ( page . get ( "snapping_increment" ) == 0 ) { page . set ( "snapping_increment" , snapInc ); } else { return ;} } }); });
1538454864
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Could also simply turn showgrid on/off and not have to worry about dealing with the snapping increment. Additionally, one critique of your code Bast: let campaign = findObjs({_type:"campaign"})[0]; This is unnecessary, you can simply use the Campaign() function and get the playerpageid from that: let page = getObj('page',Campaign().get('playerpageid'));
Ah, didn't know about that function, ty. I tried turning showgrid off/on, but while the grid lines disappeared, the snapping continued.