Page 1 of 2

lightning.lua: Thunder effect - by Elf

Posted: Fri Mar 06, 2020 8:30 pm
by Chilly14
This is not a script I made - I reposted one of Elf (a user from the PGE forums)'s scripts for more visibility. So yeah, do NOT credit me for this. Credit Elf instead, since they're the one who made it.

The script adds a customizable thunderstorm effect to the level.

GIF:
Spoiler: show
Image
FUNCTIONS:
Spoiler: show
lightning.mindelay = Mininum Delay for lightning effect. Default: 32
lightning.maxdelay = Maxinum Delay for lightning effect. Default: 324
lightning.priority = Priority of the lighting effect. Default: -99.9
lightning.speed = Fading speed of the lighting effect, the lower number, the fast speed. Default: 40
lightning.section = Section for lightning effect. Default: {0,1,2,...,20}
DOWNLOAD:
Spoiler: show

Re: lightning.lua: Thunder effect - by Elf

Posted: Sat Mar 07, 2020 7:13 am
by Emral
This code is pretty poor. Given the effect people are probably better off making their own little script. I tend to do it as such:
--------------------------
local lightningOpacity = 0

function doLightning()
lightningOpacity = 0.8
SFX.play(whateversoundiwant)
end

function onTick()
lightningOpacity = lightningOpacity - 0.02
end

function onDraw()
Graphics.drawScreen{color = Color.white .. lightningOpacity, priority = 0}
end
---------------------------
And then I just have to call doLightning periodically or whenever I want it. Of course this doesn't have all the features the library does, but it also doesn't randomly not work, and isn't exactly rocket science to set up.

So, why should and how could this code be improved?
- The section variable doesn't work as you think it does. It only works in the default setting because the sections are ordered. If I instead put {20} then, behold, the lightning will only appear in Section 0. This is because this is not a lookup table, but just a general table for enumeration. However, it is consistently used throughout the code AS a lookup table. So in my scenario section[player.section] for section 0 would be 20, which is not nil, therefore the code runs in section 0.
- The randomness is almost never going to take effect, because a new random limit is constructed each frame. This means that every frame there's essentially a 1/(maxdelay - mindelay) chance that the timer ends early. A more sensible way to approach a random interval would be to store the next limit whenever a lightning happens, once, and then refer to that throughout the next countdown.
- The "speed" variable is odd. Larger speeds cause the lightning to stick around for longer. It's akin to my lightningOpacity variable in a sense, except what you manipulate is the -0.02 decrease speed and not the opacity. However, the decrease speed ALSO is used to infer the opacity at all times. It would make more sense to name this variable something like "falloffSpeed" and add another variable that corresponds to "maxOpacity".
- The code loops over all cameras 4 times per frame, and renders the lightning for each camera for each camera. Expect the code to act weirdly when two players are active. This can be fixed by, for one, storing references to the cameras once at the start of gameplay, and two, using onDraw instead of onCameraDraw. Alternatively, onCameraDraw's passed cameraIndex parameter can be used to retrieve the appropriate camera to draw for in onCameraDraw directly.
- While the section is adjustible, most things don't care about the section. Lightning will still happen regardless, and every section is forced to have identical lightning timers and speeds. You can enter a section with lighting mid-falloff and it might be odd that nothing could be heard. To play the sound, the code loops over all players, instead of using Section.getActiveIndices() and just checking if the sections have lightning that way.

Re: lightning.lua: Thunder effect - by Elf

Posted: Sat Mar 07, 2020 10:38 am
by Chilly14
Enjl wrote:
Sat Mar 07, 2020 7:13 am
This code is pretty poor. Given the effect people are probably better off making their own little script. I tend to do it as such:
--------------------------
local lightningOpacity = 0

function doLightning()
lightningOpacity = 0.8
SFX.play(whateversoundiwant)
end

function onTick()
lightningOpacity = lightningOpacity - 0.02
end

function onDraw()
Graphics.drawScreen{color = Color.white .. lightningOpacity, priority = 0}
end
---------------------------
And then I just have to call doLightning periodically or whenever I want it. Of course this doesn't have all the features the library does, but it also doesn't randomly not work, and isn't exactly rocket science to set up.

So, why should and how could this code be improved?
- The section variable doesn't work as you think it does. It only works in the default setting because the sections are ordered. If I instead put {20} then, behold, the lightning will only appear in Section 0. This is because this is not a lookup table, but just a general table for enumeration. However, it is consistently used throughout the code AS a lookup table. So in my scenario section[player.section] for section 0 would be 20, which is not nil, therefore the code runs in section 0.
- The randomness is almost never going to take effect, because a new random limit is constructed each frame. This means that every frame there's essentially a 1/(maxdelay - mindelay) chance that the timer ends early. A more sensible way to approach a random interval would be to store the next limit whenever a lightning happens, once, and then refer to that throughout the next countdown.
- The "speed" variable is odd. Larger speeds cause the lightning to stick around for longer. It's akin to my lightningOpacity variable in a sense, except what you manipulate is the -0.02 decrease speed and not the opacity. However, the decrease speed ALSO is used to infer the opacity at all times. It would make more sense to name this variable something like "falloffSpeed" and add another variable that corresponds to "maxOpacity".
- The code loops over all cameras 4 times per frame, and renders the lightning for each camera for each camera. Expect the code to act weirdly when two players are active. This can be fixed by, for one, storing references to the cameras once at the start of gameplay, and two, using onDraw instead of onCameraDraw. Alternatively, onCameraDraw's passed cameraIndex parameter can be used to retrieve the appropriate camera to draw for in onCameraDraw directly.
- While the section is adjustible, most things don't care about the section. Lightning will still happen regardless, and every section is forced to have identical lightning timers and speeds. You can enter a section with lighting mid-falloff and it might be odd that nothing could be heard. To play the sound, the code loops over all players, instead of using Section.getActiveIndices() and just checking if the sections have lightning that way.
Okay. I'll try to contact the original author about this.

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 4:13 am
by Danat Nukem
How make this work? I even trying local lightning = require("lightning") how author saying and this ain't working anyway...

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 8:39 am
by Chilly14
Danat Nukem wrote:
Tue Mar 23, 2021 4:13 am
How make this work? I even trying local lightning = require("lightning") how author saying and this ain't working anyway...
Did you make a typo in "lightning"? Where did you put it? Also, can you show me the error?

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 10:12 am
by Danat Nukem
Chilly14 wrote:
Tue Mar 23, 2021 8:39 am
Danat Nukem wrote:
Tue Mar 23, 2021 4:13 am
How make this work? I even trying local lightning = require("lightning") how author saying and this ain't working anyway...
Did you make a typo in "lightning"? Where did you put it? Also, can you show me the error?
Thanks for answer, there is no error, lightning just not happened

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 10:15 am
by Chilly14
Danat Nukem wrote:
Tue Mar 23, 2021 10:12 am
Chilly14 wrote:
Tue Mar 23, 2021 8:39 am
Danat Nukem wrote:
Tue Mar 23, 2021 4:13 am
How make this work? I even trying local lightning = require("lightning") how author saying and this ain't working anyway...
Did you make a typo in "lightning"? Where did you put it? Also, can you show me the error?
Thanks for answer, there is no error, lightning just not happened
Can I see your code? It works fine for me.

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 10:29 am
by Danat Nukem
Chilly14 wrote:
Tue Mar 23, 2021 10:15 am
Danat Nukem wrote:
Tue Mar 23, 2021 10:12 am
Chilly14 wrote:
Tue Mar 23, 2021 8:39 am

Did you make a typo in "lightning"? Where did you put it? Also, can you show me the error?
Thanks for answer, there is no error, lightning just not happened
Can I see your code? It works fine for me.
Ye ofc, there it is
Spoiler: show
local lightning = require("lightning")

local lightningtimer = 0
local lightningcountdown = 0

lightning.mindelay = 32
lightning.maxdelay = 324
lightning.priority = -99.9
lightning.speed = 40
lightning.section = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}

local c1,c2

function lightning.onInitAPI()
registerEvent(lightning,"onTick")
registerEvent(lightning,"onCameraDraw")
end

function lightning.onTick()
if lightningtimer == RNG.randomInt(lightning.mindelay,lightning.maxdelay) or lightningtimer > lightning.maxdelay then
lightningtimer = 0
lightningcountdown = lightning.speed
else
lightningtimer = lightningtimer + 1
if lightningcountdown > 0 then
lightningcountdown = lightningcountdown - 1
else
lightningcountdown = 0
end
end
for _,p in ipairs(Player.get()) do
if lightning.section[p.section+1] ~= nil and lightningtimer == 0 and lightningcountdown == lightning.speed then
SFX.play(43)
end
end
end

function lightning.onCameraDraw()
c1 = Camera.get()[1]
c2 = Camera.get()[2]
if lightningcountdown > 0 then
if lightning.section[player.section+1] ~= nil then
Graphics.drawBox{x=c1.x,y=c1.y,width=c1.width,height=c1.height,sceneCoords=true,color=Color(1,1,1,lightningcountdown/(lightning.speed*1.125)),priority=lightning.priority}
end
if player2 and lightning.section[player2.section+1] ~= nil and c2:mem(0x20,FIELD_BOOL) then
Graphics.drawBox{x=c2.x,y=c2.y,width=c2.width,height=c2.height,sceneCoords=true,color=Color(1,1,1,lightningcountdown/(lightning.speed*1.125)),priority=lightning.priority}
end
end
end

return lightning

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 11:24 am
by Emral
DON'T copypaste the contents of the library file. You ONLY NEED your first line in your luna.lua!!! And the original lightning.lua in the same folder!

Re: lightning.lua: Thunder effect - by Elf

Posted: Tue Mar 23, 2021 12:24 pm
by Danat Nukem
Oh wow, i'm really inattentive I didn't notice it right away, and now it's working, thx

Re: lightning.lua: Thunder effect - by Elf

Posted: Mon May 03, 2021 11:37 am
by newgamer762
local lightning = require{“lightning”}

local lightningtimer = 0
local lightningcountdown = 0

lightning.mindelay = 32
lightning.maxdelay = 324
lightning.priority = -99.9
lightning.speed = 40
lightning.section = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}

local c1,c2

function lightning.onInitAPI()
registerEvent(lightning,"onTick")
registerEvent(lightning,"onCameraDraw")
end

function lightning.onTick()
if lightningtimer == RNG.randomInt(lightning.mindelay,lightning.maxdelay) or lightningtimer > lightning.maxdelay then
lightningtimer = 0
lightningcountdown = lightning.speed
else
lightningtimer = lightningtimer + 1
if lightningcountdown > 0 then
lightningcountdown = lightningcountdown - 1
else
lightningcountdown = 0
end
end
for _,p in ipairs(Player.get()) do
if lightning.section[p.section+1] ~= nil and lightningtimer == 0 and lightningcountdown == lightning.speed then
SFX.play(43)
end
end
end

function lightning.onCameraDraw()
c1 = Camera.get()[1]
c2 = Camera.get()[2]
if lightningcountdown > 0 then
if lightning.section[player.section+1] ~= nil then
Graphics.drawBox{x=c1.x,y=c1.y,width=c1.width,height=c1.height,sceneCoords=true,color=Color(1,1,1,lightningcountdown/(lightning.speed*1.125)),priority=lightning.priority}
end
if player2 and lightning.section[player2.section+1] ~= nil and c2:mem(0x20,FIELD_BOOL) then
Graphics.drawBox{x=c2.x,y=c2.y,width=c2.width,height=c2.height,sceneCoords=true,color=Color(1,1,1,lightningcountdown/(lightning.speed*1.125)),priority=lightning.priority}
end
end
end

return lightning


It's not working.....

Re: lightning.lua: Thunder effect - by Elf

Posted: Mon May 03, 2021 11:46 am
by Chilly14
Enjl wrote:
Tue Mar 23, 2021 11:24 am
DON'T copypaste the contents of the library file. You ONLY NEED your first line in your luna.lua!!! And the original lightning.lua in the same folder!

Re: lightning.lua: Thunder effect - by Elf

Posted: Fri May 07, 2021 10:35 am
by newgamer762
Chilly14 wrote:
Mon May 03, 2021 11:46 am
Enjl wrote:
Tue Mar 23, 2021 11:24 am
DON'T copypaste the contents of the library file. You ONLY NEED your first line in your luna.lua!!! And the original lightning.lua in the same folder!
I don't get it. Could you give an example?

Re: lightning.lua: Thunder effect - by Elf

Posted: Sun May 09, 2021 10:07 am
by ShadowXeldron
newgamer762 wrote:
Fri May 07, 2021 10:35 am
Chilly14 wrote:
Mon May 03, 2021 11:46 am
Enjl wrote:
Tue Mar 23, 2021 11:24 am
DON'T copypaste the contents of the library file. You ONLY NEED your first line in your luna.lua!!! And the original lightning.lua in the same folder!
I don't get it. Could you give an example?
Answering someone lese's question bwahaha Remove every line except local lightning = require{“lightning”}

Re: lightning.lua: Thunder effect - by Elf

Posted: Thu May 13, 2021 10:21 am
by newgamer762
Dragon0307 wrote:
Sun May 09, 2021 10:07 am
newgamer762 wrote:
Fri May 07, 2021 10:35 am
Chilly14 wrote:
Mon May 03, 2021 11:46 am
It's still not working......

I don't get it. Could you give an example?
Answering someone lese's question bwahaha Remove every line except local lightning = require{“lightning”}

Re: lightning.lua: Thunder effect - by Elf

Posted: Thu May 13, 2021 10:28 am
by Chilly14
newgamer762 wrote:
Thu May 13, 2021 10:21 am
Dragon0307 wrote:
Sun May 09, 2021 10:07 am
newgamer762 wrote:
Fri May 07, 2021 10:35 am


It's still not working......

I don't get it. Could you give an example?
Answering someone lese's question bwahaha Remove every line except local lightning = require{“lightning”}
It actually seems that you're using {} instead of (), as in:

Code: Select all

lightning = require(“lightning”)

Re: lightning.lua: Thunder effect - by Elf

Posted: Thu May 13, 2021 10:59 am
by ShadowXeldron
Chilly14 wrote:
Thu May 13, 2021 10:28 am
newgamer762 wrote:
Thu May 13, 2021 10:21 am
Dragon0307 wrote:
Sun May 09, 2021 10:07 am


Answering someone lese's question bwahaha Remove every line except local lightning = require{“lightning”}
It actually seems that you're using {} instead of (), as in:

Code: Select all

lightning = require(“lightning”)
Why didn't I notice something trivial like that that!? I probably won't be living that down.

Re: lightning.lua: Thunder effect - by Elf

Posted: Sat May 15, 2021 6:17 am
by newgamer762
Chilly14 wrote:
Thu May 13, 2021 10:28 am
newgamer762 wrote:
Thu May 13, 2021 10:21 am
Dragon0307 wrote:
Sun May 09, 2021 10:07 am


Answering someone lese's question bwahaha Remove every line except local lightning = require{“lightning”}
It actually seems that you're using {} instead of (), as in:

Code: Select all

lightning = require(“lightning”)
I changed the {} into () just now. But it's still not working....

Re: lightning.lua: Thunder effect - by Elf

Posted: Sat May 15, 2021 9:13 am
by ShadowXeldron
newgamer762 wrote:
Sat May 15, 2021 6:17 am
Chilly14 wrote:
Thu May 13, 2021 10:28 am
newgamer762 wrote:
Thu May 13, 2021 10:21 am
It actually seems that you're using {} instead of (), as in:

Code: Select all

lightning = require(“lightning”)
I changed the {} into () just now. But it's still not working....
Download the file and put it in your level folder (the same one where you put the graphics).

Re: lightning.lua: Thunder effect - by Elf

Posted: Mon May 17, 2021 10:26 am
by newgamer762
Dragon0307 wrote:
Sat May 15, 2021 9:13 am
newgamer762 wrote:
Sat May 15, 2021 6:17 am
Chilly14 wrote:
Thu May 13, 2021 10:28 am

It actually seems that you're using {} instead of (), as in:

Code: Select all

lightning = require(“lightning”)
I changed the {} into () just now. But it's still not working....
Download the file and put it in your level folder (the same one where you put the graphics).


Image
I don't see anything wrong with it.... :?