lightning.lua: Thunder effect - by Elf

Share and discuss custom LunaLua code and content packs for SMBX2.

Moderator: Userbase Moderators

Chilly14
Snifit
Snifit
Posts: 236
Joined: Thu Jul 18, 2019 10:28 pm
Flair: wash your hands and stay at home
Pronouns: he/him

lightning.lua: Thunder effect - by Elf

Postby Chilly14 » Fri Mar 06, 2020 8:30 pm

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:
Last edited by Chilly14 on Thu Mar 18, 2021 1:48 pm, edited 1 time in total.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9861
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: lightning.lua: Thunder effect - by Elf

Postby Emral » 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.

Chilly14
Snifit
Snifit
Posts: 236
Joined: Thu Jul 18, 2019 10:28 pm
Flair: wash your hands and stay at home
Pronouns: he/him

Re: lightning.lua: Thunder effect - by Elf

Postby Chilly14 » Sat Mar 07, 2020 10:38 am

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.

Danat Nukem
Bob-Omb
Bob-Omb
Posts: 22
Joined: Tue Aug 04, 2020 6:32 am

Re: lightning.lua: Thunder effect - by Elf

Postby Danat Nukem » 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...

Chilly14
Snifit
Snifit
Posts: 236
Joined: Thu Jul 18, 2019 10:28 pm
Flair: wash your hands and stay at home
Pronouns: he/him

Re: lightning.lua: Thunder effect - by Elf

Postby Chilly14 » 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?

Danat Nukem
Bob-Omb
Bob-Omb
Posts: 22
Joined: Tue Aug 04, 2020 6:32 am

Re: lightning.lua: Thunder effect - by Elf

Postby Danat Nukem » 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

Chilly14
Snifit
Snifit
Posts: 236
Joined: Thu Jul 18, 2019 10:28 pm
Flair: wash your hands and stay at home
Pronouns: he/him

Re: lightning.lua: Thunder effect - by Elf

Postby Chilly14 » 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
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.

Danat Nukem
Bob-Omb
Bob-Omb
Posts: 22
Joined: Tue Aug 04, 2020 6:32 am

Re: lightning.lua: Thunder effect - by Elf

Postby Danat Nukem » Tue Mar 23, 2021 10:29 am

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

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9861
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: lightning.lua: Thunder effect - by Elf

Postby Emral » 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!

Danat Nukem
Bob-Omb
Bob-Omb
Posts: 22
Joined: Tue Aug 04, 2020 6:32 am

Re: lightning.lua: Thunder effect - by Elf

Postby Danat Nukem » Tue Mar 23, 2021 12:24 pm

Oh wow, i'm really inattentive I didn't notice it right away, and now it's working, thx

newgamer762
Swooper
Swooper
Posts: 54
Joined: Fri Apr 02, 2021 1:08 pm
Pronouns: he

Re: lightning.lua: Thunder effect - by Elf

Postby newgamer762 » Mon May 03, 2021 11:37 am

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.....

Chilly14
Snifit
Snifit
Posts: 236
Joined: Thu Jul 18, 2019 10:28 pm
Flair: wash your hands and stay at home
Pronouns: he/him

Re: lightning.lua: Thunder effect - by Elf

Postby Chilly14 » 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!

newgamer762
Swooper
Swooper
Posts: 54
Joined: Fri Apr 02, 2021 1:08 pm
Pronouns: he

Re: lightning.lua: Thunder effect - by Elf

Postby newgamer762 » 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?

ShadowXeldron
Snifit
Snifit
Posts: 232
Joined: Sun Dec 08, 2019 8:21 am
Flair: phpBB > ProBoards
Pronouns: He/Him
Contact:

Re: lightning.lua: Thunder effect - by Elf

Postby ShadowXeldron » 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
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”}

newgamer762
Swooper
Swooper
Posts: 54
Joined: Fri Apr 02, 2021 1:08 pm
Pronouns: he

Re: lightning.lua: Thunder effect - by Elf

Postby newgamer762 » 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
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”}

Chilly14
Snifit
Snifit
Posts: 236
Joined: Thu Jul 18, 2019 10:28 pm
Flair: wash your hands and stay at home
Pronouns: he/him

Re: lightning.lua: Thunder effect - by Elf

Postby Chilly14 » 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
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”)

ShadowXeldron
Snifit
Snifit
Posts: 232
Joined: Sun Dec 08, 2019 8:21 am
Flair: phpBB > ProBoards
Pronouns: He/Him
Contact:

Re: lightning.lua: Thunder effect - by Elf

Postby ShadowXeldron » Thu May 13, 2021 10:59 am

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.

newgamer762
Swooper
Swooper
Posts: 54
Joined: Fri Apr 02, 2021 1:08 pm
Pronouns: he

Re: lightning.lua: Thunder effect - by Elf

Postby newgamer762 » 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
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....

ShadowXeldron
Snifit
Snifit
Posts: 232
Joined: Sun Dec 08, 2019 8:21 am
Flair: phpBB > ProBoards
Pronouns: He/Him
Contact:

Re: lightning.lua: Thunder effect - by Elf

Postby ShadowXeldron » 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
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).

newgamer762
Swooper
Swooper
Posts: 54
Joined: Fri Apr 02, 2021 1:08 pm
Pronouns: he

Re: lightning.lua: Thunder effect - by Elf

Postby newgamer762 » Mon May 17, 2021 10:26 am

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.... :?


Return to “LunaLua”

Who is online

Users browsing this forum: spamtonV420 and 6 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari