Page 1 of 1

Need Help with this Messy Code

Posted: Sun Aug 10, 2025 6:30 pm
by PizzaNoob
Ok, so I'm making a title screen with a title menu. For some reason when "pg.settings.artDispOpen" or "pg.settings.soundTestOpen" are true, the code opens the main title menu (sets "tm.settings.open" to true). I don't know what to do or how I can optimize the code. As of now, this code is being spread accross THREE scripts, one of them being the title level's luna.lua file, one for the main title menu, and one for the gallery. So any better coders reading this, please help me because I'm VERY close to giving up yet I don't want to because I got this far.

Luna Lua
Spoiler: show
--No Pausing and No HUD--
local customPause = require("Scripts/customPause")
customPause.settings.canPause = false

local customReserve = require("Scripts/customReserve")
Graphics.activateHud(false)
customReserve.draw = false
customReserve.canUse = false
-------------------------

--Loadin' Stuff--
local textplus = require("textplus")
local easing = require("ext/easing")
local tm = require("Scripts/titleMenu")
--local ps = require("Scripts/pyraSettings")
local pg = require("Scripts/pyraGallery")
-----------------

--Global/Misc Stuff--
local font = textplus.loadFont("SGraphics/fonts/pyraFont.ini")

local screenOpac = 1
local textOpac = 1

local timer = 0

local sound = false
local menuAction = false
local musicPlayed = false
---------------------

--Logo Properties--
--Image
local logo = Graphics.loadImageResolved("TitleStuff/gameName.png")
--Positions
local logoX = 280
local logoY = 100
-- Opacity
local logoOpac = 1
local logoTargetOpac = 1
local textTargetOpac = 1
local fadeSpeed = 0.05
local isFadingIn = false
--Easing
local logoSpeed = 0.005
local logoEaseTimer = 0
local startOffset = 700
local currentOffset = 0
-------------------

--Presents Properties--
--Image
local presents = Graphics.loadImageResolved("TitleStuff/presents.png")
--Positions
local presentsX = 368
local presentsY = 144
--Opacity
local presentsOpac = 0
--Presents Jingle
local jingleAudioFile = Misc.resolveSoundFile("SFX/jingles/presentsJingle.ogg")
local preJingle = false
-----------------------

--Text Properties--
local textPos = 690
-------------------

--On Tick Function--
function onTick()
--Jump Related
player.keys.jump = false
player.keys.altJump = false
--Run Related
player.keys.run = false
player.keys.altRun = false
--Item Box
player.keys.dropItem = false
--Directional Keys
player.keys.left = false
player.keys.right = false
player.keys.up = false
player.keys.down = false

--Timer adds up
timer = math.clamp(timer + 0.02, 0, 14)

if timer >= 0 then
logoY = startOffset
end

if screenOpac <= 0 then
logoY = 100
logoEaseTimer = math.min(logoEaseTimer + logoSpeed, 1)
local ev = easing.outBack(logoEaseTimer, 0, 1, 1)
currentOffset = math.lerp(startOffset, 0, ev)
end

if timer >= 1.5 then
presentsOpac = math.clamp(presentsOpac + fadeSpeed * 1, 0, 1)
if not preJingle then
SFX.play(jingleAudioFile)
preJingle = true
end
end

if timer >= 3.5 then
presentsOpac = math.clamp(presentsOpac + fadeSpeed * -2, 0, 1)
if not musicPlayed then
triggerEvent("mainTheme")
musicPlayed = true
end
end

if timer >= 6.2 then
screenOpac = screenOpac - 0.02
end

if timer >= 12 then
textPos = math.lerp(textPos,500,0.1)
end

if timer >= 13.5 then
if sound then
logoOpac = math.clamp(logoOpac + fadeSpeed * -1, 0, 1)
textOpac = math.clamp(textOpac + fadeSpeed * -1, 0, 1)
end
end

if isFadingIn then
logoOpac = math.min(logoOpac + fadeSpeed, logoTargetOpac)
textOpac = math.min(textOpac + fadeSpeed, textTargetOpac)

if logoOpac >= logoTargetOpac and textOpac >= textTargetOpac then
isFadingIn = false
end
end

if tm.settings.open and menuAction then
menuAction = false
end
end
--------------------

--Input Stuff--
function onInputUpdate()
if player.rawKeys.jump == KEYS_PRESSED and timer >= 13.5 and not tm.settings.open and not pg.settings.open and not pg.settings.artDispOpen and tm.settings.returnDelay == 0 then
if not sound then
SFX.play(12)
sound = true
end
Routine.run(
function()
triggerEvent("musicFade")
Routine.wait(1.5)
musicPlayed = false
tm.settings.open = true
triggerEvent("menus")
end
)
end
end
---------------

--On Draw Function--
function onDraw()
--Covering Screen
Graphics.drawBox{
color = Color.black..screenOpac,
x = 0,
y = 0,
width = camera.width,
height = camera.height,
priority = -10,
}

--Game Logo
Graphics.drawBox{
texture = logo,
x = logoX,
y = logoY - currentOffset,
priority = -5,
color = Color.white..logoOpac,
}

--Presents Thingy
Graphics.drawBox{
texture = presents,
x = presentsX,
y = presentsY,
priority = -5,
color = Color.white..presentsOpac,
}


--Press JUMP text
textplus.print{
text = "Press JUMP",
x = logoX + 150,
y = textPos - 20,
xscale = 1,
yscale = 1,
font = font,
color = Color(textOpac,textOpac,textOpac,textOpac),
priority = -10
}

--Hide player
player.frame = -50
end
--------------------

--On Event Function--
function onEvent(e)
if e == "mainTheme" then
Audio.MusicChange(0,("music/mainTheme.ogg"))
elseif e == "mainThemePos" then
Audio.MusicChange(0,("music/mainTheme.ogg"),1000)
Audio.MusicSetPos(3.4)
elseif e == "menus" then
Audio.MusicChange(0,("music/menus.ogg"))
elseif e == "musicFade" then
Audio.MusicFadeOut(0,500)
end
end
---------------------
Title Menu
Spoiler: show
--Loadin' Stuff--
local textplus = require("textplus")
local easing = require("ext/easing")
-----------------

--Looping Shader--
local loopShader = Shader()
loopShader:compileFromFile(nil, Misc.resolveFile("shaders/basic/shader_loop.frag"))
------------------

--The other menus--
local pg
pcall(function() pg = require("Scripts/pyraGallery") end)
-------------------

--Global/Misc Stuff--
--Font for "Buttons"
local font = textplus.loadFont("SGraphics/fonts/marioFont.ini")

--Opacity Values
local textOpac = 0
local boardOpac = 0
local screenOpac = 0

--Height for rotated headers
local screenHeight = 0
-----------------

--Character Color for the boarders of the title menu--
local charColor = {
[1] = 0x3c0000,
[2] = 0x00370b,
[4] = 0x010056
}
------------------------------------------------------

--Character Swapping Sounds--
local playerSound = {
[1] = Misc.resolveSoundFile("SFX/Mario/Okie Dokie.wav"),
[2] = Misc.resolveSoundFile("SFX/Luigi/Oh Yeah.wav"),
[4] = Misc.resolveSoundFile("SFX/Toad/Yay.wav"),
}
-----------------------------

local tm = {}

--Main Settings--
tm.settings = {
open = false,
isLeaving = false,
doTextReturn = false,
returnDelay = 0,
selectedOption = 1,
}
-----------------

--Da "Buttons"--
tm.buttons = {
--Start
{
text = "Start Game",
x = 70,
y = 100,
func = function()
tm.settings.open = false
Routine.run(
function()
triggerEvent("musicFade")
SFX.play(Misc.resolveSoundFile("SFX/basicSounds/menuClick.ogg"))
Routine.wait(0.8)
tm.settings.isLeaving = true
end
)
end
},
--Gallery
{
text = "Gallery",
x = 80,
y = 230,
func = function()
tm.settings.open = false
Routine.run(
function()
SFX.play(Misc.resolveSoundFile("SFX/basicSounds/menuClick.ogg"))
Routine.wait(1)
pg.settings.open = true
end
)
end
},
--Options
{
text = "Options",
x = 90,
y = 360,
func = function()
SFX.play(Misc.resolveSoundFile("SFX/basicSounds/menuClick.ogg"))
end
},
--Exit
{
text = "Exit Game",
x = 100,
y = 490,
func = function()
tm.settings.open = false
Routine.run(
function()
triggerEvent("musicFade")
SFX.play(Misc.resolveSoundFile("SFX/basicSounds/menuClick.ogg"))
Routine.wait(0.8)
tm.settings.isLeaving = true
end
)
end
}
}

--Easing for the Options--
local easeTimer = 0
local easeSpeed = 0.01
local currentOffset = 0
local startOffset = 400
--------------------------

--Registration--
registerEvent(tm, "onTick")
registerEvent(tm, "onDraw")
registerEvent(tm, "onInputUpdate")
----------------

--On Tick Function--
function tm.onTick()
if tm.settings.open then
easeTimer = math.min(easeTimer + easeSpeed, 1)
local ev = easing.outBack(easeTimer, 0, 1, 1)
currentOffset = math.lerp(startOffset, 0, ev)

tm.settings.returnDelay = math.clamp(tm.settings.returnDelay + 0.02 * 1, 0, 1.5)
screenHeight = math.lerp(screenHeight,64,0.1)
boardOpac = math.clamp(boardOpac + 0.02 * 1, 0, 1)
textOpac = math.clamp(textOpac + 0.03 * 1, 0, 1)
else
tm.settings.returnDelay = math.clamp(tm.settings.returnDelay + 0.02 * -1, 0, 1.5)
screenHeight = math.lerp(screenHeight,0,0.1)
boardOpac = math.clamp(boardOpac + 0.02 * -1, 0, 1)
textOpac = math.clamp(textOpac + 0.03 * -1, 0, 1)
end

if tm.settings.isLeaving then
screenOpac = screenOpac + 0.02
end

if screenOpac >= 1.5 and tm.settings.selectedOption == 1 then
Level.exit()
end

if screenOpac >= 1.5 and tm.settings.selectedOption == 4 then
Misc.exitEngine()
end
end
--------------------

--On Draw Function--
function tm.onDraw()
--The Main stuff
local board = Graphics.loadImageResolved("titleStuff/boarder-"..player.character..".png")
Graphics.drawBox{
texture = board,
x = -100,
y = camera.height + 200,
sourceX = (lunatime.tick()/6 - camera.width) * 3,
sourceWidth = camera.width,
priority = -12,
rotation = -110,
color = Color.white..boardOpac,
shader = loopShader,
}

local back = Graphics.loadImageResolved("titleStuff/back-"..player.character..".png")
Graphics.drawBox{
texture = back,
x = 0,
y = 0,
sourceX = (lunatime.tick()/6 - camera.width) * 2,
sourceY = (lunatime.tick()/6 - camera.height) * 2,
sourceWidth = camera.width,
sourceHeight = camera.height * 100,
priority = -13,
color = Color.white..boardOpac,
shader = loopShader,
}

Graphics.drawBox{
color = Color.fromHexRGB(charColor[player.character]),
x = -10,
y = 0,
width = camera.width,
height = screenHeight,
priority = -10,
rotation = -5,
}

Graphics.drawBox{
color = Color.fromHexRGB(charColor[player.character]),
x = 10,
y = camera.height + 100 - screenHeight,
width = camera.width,
height = screenHeight,
priority = -10,
rotation = -5,
}

--Buttons
for k, data in ipairs(tm.buttons) do
local textColor = Color(textOpac,textOpac,textOpac,textOpac,textOpac)
--Checks which option is selected and changes the color
if k == tm.settings.selectedOption then
textColor = Color.yellow*textOpac
end

textplus.print{
text = data.text,
x = data.x - currentOffset,
y = data.y,
xscale = 2,
yscale = 2,
font = font,
color = textColor,
priority = -4
}
end

--Covering Screen
Graphics.drawBox{
color = Color.black..screenOpac,
x = 0,
y = 0,
width = camera.width,
height = camera.height,
priority = -1,
}
end
--------------------

--Input stuff--
function tm.onInputUpdate()
if tm.settings.open and not tm.settings.isLeaving and tm.settings.returnDelay == 1.5 then
--Vertical Keys
if player.rawKeys.up == KEYS_PRESSED then
tm.settings.sound = false
SFX.play(26)
tm.settings.selectedOption = tm.settings.selectedOption - 1
if tm.settings.selectedOption < 1 then
tm.settings.selectedOption = #tm.buttons
end
elseif player.rawKeys.down == KEYS_PRESSED then
tm.settings.sound = false
SFX.play(26)
tm.settings.selectedOption = tm.settings.selectedOption + 1
if tm.settings.selectedOption > #tm.buttons then
tm.settings.selectedOption = 1
end
end

--Horizontal Keys
if player.rawKeys.left == KEYS_PRESSED then
if player.character == 1 then
SFX.play{sound = playerSound[4], volume = 0.5}
player:transform(4)
elseif player.character == 2 then
SFX.play{sound = playerSound[1], volume = 0.5}
player:transform(1)
elseif player.character == 4 then
SFX.play{sound = playerSound[2], volume = 0.5}
player:transform(2)
end
elseif player.rawKeys.right == KEYS_PRESSED then
if player.character == 1 then
SFX.play{sound = playerSound[2], volume = 0.5}
player:transform(2)
elseif player.character == 2 then
SFX.play{sound = playerSound[4], volume = 0.5}
player:transform(4)
elseif player.character == 4 then
SFX.play{sound = playerSound[1], volume = 0.5}
player:transform(1)
end
end

--Jump Key
if player.rawKeys.jump == KEYS_PRESSED then
if tm.buttons[tm.settings.selectedOption] and tm.buttons[tm.settings.selectedOption].func then
tm.buttons[tm.settings.selectedOption].func()
end
end
end

--Gallery Stuff
if pg.settings.open and not pg.settings.leavingStuff and tm.settings.returnDelay == 0 then
if player.rawKeys.run == KEYS_PRESSED then
Routine.run(
function()
pg.settings.open = false
Routine.wait(1)
tm.settings.open = true
end
)
end
end
end
---------------

return tm
Gallery
Spoiler: show
--Loadin' Stuff--
local textplus = require("textplus")
local easing = require("ext/easing")
-----------------

--Looping Shader--
local loopShader = Shader()
loopShader:compileFromFile(nil, Misc.resolveFile("shaders/basic/shader_loop.frag"))
------------------

--Global/Misc Stuff--
--Font for "Buttons"
local font = textplus.loadFont("SGraphics/fonts/marioFont.ini")
--Opacity Values
local boardOpac = 0
local textOpac = 0
local boardOpac2 = 0
--Height for rotated headers
local screenHeight = 0
local screenHeight2 = 0
---------------------

--Easing for the Options--
local easeTimer = 0
local easeSpeed = 0.01
local currentOffset = 0
local startOffset = 400
--------------------------

local pg = {}

--Main Settings--
pg.settings = {
open = false,
artDispOpen = false,
soundTestOpen = false,
sound = false,
selectedOption = 1,
leavingStuff = false
}
-----------------

--The only 2 buttons--
pg.buttons = {
--Art
{
text = "Art Section",
x = 100,
y = 300,
func = function()
SFX.play(Misc.resolveSoundFile("SFX/basicSounds/menuClick.ogg"))
Routine.run(
function()
pg.settings.open = false
Routine.wait(1)
pg.settings.artDispOpen = true
end
)
end
},
--Music
{
text = "Sound Test",
x = 600,
y = 300,
func = function()
SFX.play(Misc.resolveSoundFile("SFX/basicSounds/menuClick.ogg"))
Routine.run(
function()
--pg.settings.open = false
--pg.settings.soundTestOpen = true
end
)
end
},
}
----------------------

--Registration--
registerEvent(pg, "onTick")
registerEvent(pg, "onDraw")
registerEvent(pg, "onInputUpdate")
----------------

--On Tick Function--
function pg.onTick()
if pg.settings.open then
easeTimer = math.min(easeTimer + easeSpeed, 1)
local ev = easing.outBack(easeTimer, 0, 1, 1)
currentOffset = math.lerp(startOffset, 0, ev)
textOpac = math.clamp(textOpac + 0.03 * 1, 0, 1)
boardOpac = math.clamp(boardOpac + 0.02 * 1, 0, 1)
screenHeight = math.lerp(screenHeight,64,0.1)
else
screenHeight = math.lerp(screenHeight,0,0.1)
textOpac = math.clamp(textOpac + 0.03 * -1, 0, 1)
boardOpac = math.clamp(boardOpac + 0.02 * -1, 0, 1)
end

if pg.settings.artDispOpen then
boardOpac2 = math.clamp(boardOpac2 + 0.02 * 1, 0, 1)
screenHeight2 = math.lerp(screenHeight2,64,0.1)
else
screenHeight2 = math.lerp(screenHeight2,0,0.1)
boardOpac2 = math.clamp(boardOpac2 + 0.02 * -1, 0, 1)
end
end
--------------------

--On Draw Function--
function pg.onDraw()
--Main Part--
local back = Graphics.loadImageResolved("titleStuff/galleryBack.png")
Graphics.drawBox{
texture = back,
x = 0,
y = 0,
sourceX = (lunatime.tick()/6 - camera.width) * 2,
sourceY = (lunatime.tick()/6 - camera.height) * 2,
sourceWidth = camera.width,
sourceHeight = camera.height * 100,
priority = -13,
color = Color.white..boardOpac,
shader = loopShader,
}

Graphics.drawBox{
color = Color.fromHexRGB(0x2f0056),
x = -10,
y = 0,
width = camera.width,
height = screenHeight,
priority = -10,
rotation = -5,
}

Graphics.drawBox{
color = Color.fromHexRGB(0x2f0056),
x = 10,
y = camera.height + 100 - screenHeight,
width = camera.width,
height = screenHeight,
priority = -10,
rotation = -5,
}

for k, data in ipairs(pg.buttons) do
local textColor = Color.white*textOpac
--Checks which option is selected and changes the color
if k == pg.settings.selectedOption then
textColor = Color.yellow*textOpac
end

textplus.print{
text = data.text,
x = data.x,
y = data.y - currentOffset,
xscale = 2,
yscale = 2,
font = font,
color = textColor,
priority = -4
}
end

--Art Display--
local artBack = Graphics.loadImageResolved("titleStuff/asBack.png")
Graphics.drawBox{
texture = artBack,
x = 0,
y = 0,
sourceX = (lunatime.tick()/6 - camera.width) * 2,
sourceY = (lunatime.tick()/6 - camera.height) * 2,
sourceWidth = camera.width,
sourceHeight = camera.height * 100,
priority = -13,
color = Color.white..boardOpac2,
shader = loopShader,
}

Graphics.drawBox{
color = Color.fromHexRGB(0x00485c),
x = -10,
y = 0,
width = camera.width,
height = screenHeight2,
priority = -10,
rotation = -5,
}

Graphics.drawBox{
color = Color.fromHexRGB(0x00485c),
x = 10,
y = camera.height + 100 - screenHeight2,
width = camera.width,
height = screenHeight2,
priority = -10,
rotation = -5,
}

--Sound Test--
end
--------------------

--Input stuff--
function pg.onInputUpdate()
if pg.settings.open and not pg.settings.artDispOpen then
if player.rawKeys.left == KEYS_PRESSED then
SFX.play(26)
pg.settings.selectedOption = pg.settings.selectedOption - 1
if pg.settings.selectedOption < 1 then
pg.settings.selectedOption = #pg.buttons
end
elseif player.rawKeys.right == KEYS_PRESSED then
SFX.play(26)
pg.settings.selectedOption = pg.settings.selectedOption + 1
if pg.settings.selectedOption > #pg.buttons then
pg.settings.selectedOption = 1
end
end

if player.rawKeys.jump == KEYS_PRESSED then
if pg.buttons[pg.settings.selectedOption] and pg.buttons[pg.settings.selectedOption].func then
pg.buttons[pg.settings.selectedOption].func()
end
end

if player.rawKeys.run == KEYS_PRESSED then
easeTimer = 0
end
end

if pg.settings.artDispOpen and not pg.settings.open then
if player.rawKeys.run == KEYS_PRESSED then
pg.settings.artDispOpen = false
pg.settings.open = true
Routine.run(
function()
pg.settings.leavingStuff = true
Routine.wait(1)
pg.settings.leavingStuff = false
end
)
end
elseif pg.settings.soundTestOpen and not pg.settings.open then
if player.rawKeys.run == KEYS_PRESSED then
pg.settings.soundTestOpen = false
pg.settings.open = true
Routine.run(
function()
pg.settings.leavingStuff = true
Routine.wait(1)
pg.settings.leavingStuff = false
end
)
end
end
end
---------------

return pg
EDIT: Some code is updated. I'm still having problems with "tm.settings.open" having a mind of its own. If there's a better way to approach creating a script like this OR you have a solution to the problem mentioned, please reply (these scripts are driving me crazy and I don't wan't to ditch the idea of title menus for my episode because I've gotten this far, why give up?)

Re: Need Help with this Messy Code

Posted: Wed Aug 20, 2025 1:49 pm
by PizzaNoob
Nevermind, I found a better way to approach this, I'm keeping this up though in case you want to give your feedback because, tbh, this is alot of code to take in and, if you saw my latest post about DAWs, I'm abit distracted on what I should use to make music for my project.