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

[Script,Snippet] ToggleDaylight -- turn daylight mode on, off, or toggle it for LDL or UDL on the current page.

1611173793
The Aaron
Roll20 Production Team
API Scripter
Here's a little script snippet I wrote to help with a problem.  It just toggles global illumination / daylight mode on and off.  It detects if UDL or LDL is in use, and sets the right thing. !toggle-daylight !toggle-daylight on !toggle-daylight false You can specify a parameter that will tell it to set to on or off explicitly.  Omitting the parameter will just toggle it back and forth.  You can use any of these for on: on, yes, y, true, t or any number other than 0.  Anything else is treated as off. Code: on('ready',()=>{ const getPageForPlayer = (playerid) => { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage') || Campaign().get('playerpageid'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }; on('chat:message',msg=>{ if('api'===msg.type && /^!toggle-daylight(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname'); let pid = getPageForPlayer(msg.playerid); let page = getObj('page',pid); let args = msg.content.split(/\s+/); let isSet = false; let setValue = true; if(args.length>1){ isSet = true; setValue = ['on','yes','y','true','t'].includes(args[1].toLowerCase()) || ((parseInt(args[1])||0)>1); } let field = ''; if(page.get('dynamic_lighting_enabled')) { field = 'daylight_mode_enabled'; } else if(page.get('showlighting')) { field = 'lightglobalillum'; } if(field) { let curr = page.get(field); setValue = (isSet ? setValue : !curr); if(setValue !== curr){ page.set(field, setValue); sendChat('',`/w "${who}" Daylight set to ${setValue ? 'On' : 'Off'}`); } } else { sendChat('',`/w "${who}" Dynamic Lighting not in use on this page.`); } } }); });
I was JUST thinking to myself yesterday that it would be really nice to be able to toggle global illumination in situations when I'm not sure at what time of day the party will encounter X monsters on Y map. Thank you for this, Aaron!
Curious, if its possible from here to toggle Dynamic Lighting. Originally was using !Torch to achieve this, though it's not compatible with UDL just yet.
1613929860
The Aaron
Roll20 Production Team
API Scripter
You mean just turn it off and on directly, rather than turning off and on global illumination?  This script doesn't do that, but one could certainly be written.  What's your use case, just so I can be sure I hit it?
1613931154

Edited 1613931174
The Aaron said: You mean just turn it off and on directly, rather than turning off and on global illumination?  This script doesn't do that, but one could certainly be written.  What's your use case, just so I can be sure I hit it? Correct. An example; I have town(s) with removable roof tokens. When dynamic lighting is disabled, they only see the tops of buildings. Once inside a building, I re-activate dynamic lighting so the walls become visible again and they only see the interior of the current building.
1613931646
The Aaron
Roll20 Production Team
API Scripter
Ok.  Here ya go: !toggle-udl You add an argument for on or off just as the above script to explicitly set it to one or the other. Code: on('ready',()=>{ const getPageForPlayer = (playerid) => { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage') || Campaign().get('playerpageid'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }; on('chat:message',msg=>{ if('api'===msg.type && /^!toggle-udl(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname'); let pid = getPageForPlayer(msg.playerid); let page = getObj('page',pid); let args = msg.content.split(/\s+/); let isSet = false; let setValue = true; if(args.length>1){ isSet = true; setValue = ['on','yes','y','true','t'].includes(args[1].toLowerCase()) || ((parseInt(args[1])||0)>1); } let curr = page.get('dynamic_lighting_enabled'); setValue = (isSet ? setValue : !curr); if(setValue !== curr){ page.set('dynamic_lighting_enabled', setValue); sendChat('',`/w "${who}" UDL set to ${setValue ? 'On' : 'Off'}`); } } }); });
Awesome, this is a huge boon and major time saver with UDL. Thanks for helping to make this lighting transition all the more smoother.
1613933115
The Aaron
Roll20 Production Team
API Scripter
No problem!  Definitely hit me up with any more pain points, I'm looking at making a fresh lights and vision script to replace Torch and make managing vision easier.  Any problems you experience or ideas you have would be great input. =D
1613934747

Edited 1613934777
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Level2Gamers said: The Aaron said: You mean just turn it off and on directly, rather than turning off and on global illumination?  This script doesn't do that, but one could certainly be written.  What's your use case, just so I can be sure I hit it? Correct. An example; I have town(s) with removable roof tokens. When dynamic lighting is disabled, they only see the tops of buildings. Once inside a building, I re-activate dynamic lighting so the walls become visible again and they only see the interior of the current building. You might be interested in the script Simple Roof Control . It does this automatically when a player crosses a given token (like a door). I set up a whole section of a town like this. Of course, the DL controls will probably need to be re-written when LDL goes away. Original thread .
You might be interested in the script Simple Roof Control . It does this automatically when a player crosses a given token (like a door). I set up a whole section of a town like this. Of course, the DL controls will probably need to be re-written when LDL goes away. Original thread . Whoa, never knew about this one. I'll look into this and keep it on my radar for when the conversion for UDL is available. Thanks Keith!
1613942057
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
It's pretty cool. Although the DL settings affect the entire page, of course. So as soon as someone walks into a building, any players outside will see the DL view of outside. Of course, the door is now "open", so they should be able to follow.