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

Simulate lightning

January 27 (3 years ago)

Looking for help simulating lightning with UDL or any other method. Not the most knowledgeable so if someone could break it down for me that would be great. 

January 27 (3 years ago)

Edited January 27 (3 years ago)
David M.
Pro
API Scripter

I wrote a lightning script a while ago that flashed at random intervals, though it was written for LDL. It could be modified for UDL by changing the name of the token properties in the flashOn function (those properties can be found here - emits_bright_light, bright_light_distance, emits_low_light, low_light_distance).

Note that there was a bug in UDL that prevented immediate application of light changes that I think was resolved, though I can't remember at the moment, so YMMV. 

EDIT - I can't compile & test right now, but if you have any issues I can try to get to it later.

January 27 (3 years ago)

sorry to ask , when you say changing the name of the token properties in the flashOn function which do you mean , sorry to ask but if you could explain i would appreciate it very much


January 27 (3 years ago)

Edited January 27 (3 years ago)
David M.
Pro
API Scripter

Looks like my edit and your response cross-posted! There are actually two functions to edit.

Here's the LDL versions (found in the code posted in the link above):

   const flashOff = function(src) {
        src.set('light_radius', 0);
    }
    
    const flashOn = function(src, dim, timeout) {
        let rand = Math.random() * timeout;
        
        if (dim) {
            src.set('light_dimradius', -15);
            src.set('light_radius', 200);
        } else {
            src.set('light_radius', 200);
        }
        setTimeout(flashOff, rand, src);
    }

Try replacing them with this (untested)

  const flashOff = function(src) {
        src.set('emits_bright_light', false);
        src.set('emits_low_light', false);
    }

  const flashOn = function(src, dim, timeout) {
        let rand = Math.random() * timeout;
        
        if (dim) {
            src.set('emits_low_light', true);
            src.set('low_light_distance', 400);
        } else {
            src.set('emits_bright_light', true);
            src.set('bright_light_distance', 200);
        }
        setTimeout(flashOff, rand, src);
    }

If this doesn't work, let me know and I can check it out after work tonight.

January 27 (3 years ago)

No luck , at least not on my part but I am still new to all this stuff . I set up the scene like the example you gave used !Lightning on but no effect.

January 27 (3 years ago)

Edited January 30 (3 years ago)
The Aaron
Roll20 Production Team
API Scripter

Here's a little API called Flash that simulates a lightning strike by turning on daylight mode for 300ms

Trigger it with:

!flash

Code (Fixed):

 on('ready',()=>{
  const FLASH_TIME = 300; // milliseconds

  const getPageForPlayer = (playerid) => {
    let player = getObj('player',playerid);
    if(playerIsGM(playerid)){
      return player.get('lastpage');
    }

    let psp = Campaign().get('playerspecificpages');
    if(psp[playerid]){
      return psp[playerid];
    }

    return Campaign().get('playerpageid');
  };

  const flash = (page) => {
    if(page.get('dynamic_lighting_enabled') && !page.get('daylight_mode_enabled')){
      page.set({daylight_mode_enabled: true});
      setTimeout(()=>page.set({daylight_mode_enabled: false}), FLASH_TIME);
    }
  }

  on('chat:message',(msg)=>{
    if('api' === msg.type && /^!flash(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){
      let page = getObj('page', getPageForPlayer(msg.playerid));
      if(page){
        flash(page);
        let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
        sendChat('',`/w "${who}" ⚡ FLASH ⚡`);
      }
    }
  });

});


January 27 (3 years ago)

thank you for your help both of you, I just cant get them to work , i think im going to have to do some more reading up on this stuff im just not understanding what im doing wrong

January 27 (3 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

Just in case it is a basic installation problem, here is a very quick and useful introduction to APIs:

How to Install Roll20 API Scripts

January 27 (3 years ago)

thank you, yes unfortunately I have installed the scripts they just don't seem to activate or turn on when I'm in game. Unfortunately it seems i will have to do without for next session and keep reading up on how API's work

January 27 (3 years ago)
Kraynic
Pro
Sheet Author


kyle said:

thank you, yes unfortunately I have installed the scripts they just don't seem to activate or turn on when I'm in game. Unfortunately it seems i will have to do without for next session and keep reading up on how API's work


Just to be sure...  While you are logged into the game, have you opened the api settings page up in another browser tab and hit the button to restart the api sandbox?  Then you can see if there are any errors when it restarts.

January 27 (3 years ago)

No errors :( sorry guys i know it will be something ridiculous and small that I'm doing or not doing but unfortunately I just don't know enough to realise what it is yet. 

January 27 (3 years ago)

How are you testing the actual viewing of the lightning?  I strongly recommend using a Dummy Player Account, as what you see as GM will be impacted by your GM-level viewing settings (such as the opacity of the GM layer) as well bugs that still exist with UDL. It's possible that the lightning is showing correctly for your players, but you're not seeing it as GM.

January 27 (3 years ago)

@Aaron I try your script but it did not work. I check the sandbox no error, I select the map with the ribbon. I activate by with !flash on chat but nothing happen. UDL was on, and the token had vision. 

January 27 (3 years ago)

Edited January 27 (3 years ago)
The Aaron
Roll20 Production Team
API Scripter

Do you get the message "⚡️FLASH⚡️" in the chat?

January 27 (3 years ago)

No, I try everything that I could think. once I enter !flash on chat nothing happen.

January 28 (3 years ago)

same here tried all i could think of. 


kyle said:

same here tried all i could think of. 


same

January 30 (3 years ago)
The Aaron
Roll20 Production Team
API Scripter

Ok, I got on and tested this.  Turns out I'd completely borked that script at some point, here's a new version that I just tested which works for me:

!flash
Script:

on('ready',()=>{
  const FLASH_TIME = 300; // milliseconds

  const getPageForPlayer = (playerid) => {
    let player = getObj('player',playerid);
    if(playerIsGM(playerid)){
      return player.get('lastpage');
    }

    let psp = Campaign().get('playerspecificpages');
    if(psp[playerid]){
      return psp[playerid];
    }

    return Campaign().get('playerpageid');
  };

  const flash = (page) => {
    if(page.get('dynamic_lighting_enabled') && !page.get('daylight_mode_enabled')){
      page.set({daylight_mode_enabled: true});
      setTimeout(()=>page.set({daylight_mode_enabled: false}), FLASH_TIME);
    }
  }

  on('chat:message',(msg)=>{
    if('api' === msg.type && /^!flash(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){
      let page = getObj('page', getPageForPlayer(msg.playerid));
      if(page){
        flash(page);
        let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
        sendChat('',`/w "${who}" ⚡ FLASH ⚡`);
      }
    }
  });

});


January 31 (3 years ago)
@The Aaron, Thank you it works great :) 
January 31 (3 years ago)
The Aaron
Roll20 Production Team
API Scripter

I wrote another version that uses the UDL daylightModeOpacity to fade back to darkness (or the current opacity setting) that looks awesome, but unfortunately seems to be broken in the API for client updates. =(  Hopefully that will get fixed and I can release it.

January 31 (3 years ago)

hopefully, from what ive found it seems there are a few ways to do what im after in the older version i hope they update the UDL soon. 

Are there certain settings I need to have to have this work?

January 31 (3 years ago)
The Aaron
Roll20 Production Team
API Scripter

It works on a page set up for UDL lighting.  You need to not have Global Illumination on for it to activate.

February 01 (3 years ago)

Well I am doing something wrong lol.

The Aaron said:

It works on a page set up for UDL lighting.  You need to not have Global Illumination on for it to activate.