fixed

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

fixed

Post by uknowntgyallyonski »

sorted
Last edited by uknowntgyallyonski on Thu Mar 07, 2024 12:13 am, edited 2 times in total.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: help cleaning up my code

Post by BrotSagtMist »

Start by using [co​de][/co​de] in this forum. :x

Then, why are you creating functions that only contain 1-3 lines of code?
There is no point adding extra lines just for the sake of splattering functionality all over the place.
obey
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: help cleaning up my code

Post by RNavega »

TBH, the way that they create many threads, how they don't use the code tags, plus asking basic questions that hint to a skillset level where they shouldn't be able to write multi-state game code in the first place, makes me think that they did not write this code but got it from somewhere else, like an AI.

Edit: hm, also the way that they phrased their question: "make it more presentable". For whom? Your instructor or teacher? I'm not against people asking for help with homework / assignments, but to not mention it makes it seem like you're trying to get others to do your homework for you.
Last edited by RNavega on Wed Mar 06, 2024 10:04 pm, edited 2 times in total.
Xugro
Party member
Posts: 114
Joined: Wed Sep 29, 2010 8:14 pm

Re: help cleaning up my code

Post by Xugro »

I would suggest the opposite of BrotSagtMist:
Create more functions that only contain 1-3 lines of code for better readability.

As an example your function love.mousepressed() has 5 indentation levels. With some easy refactorings you can get this down to two or three and have an easier time to read the functions:

Code: Select all

function love.mousepressed(x, y, button)
    if gameState == "menu" or gameState == "difficulty" or gameState == "instructions" then
        local leftMouseButton = 1
        if button == leftMouseButton then
            handleMouseOutsideOfPlaying(x, y)
        end
    end
end

function handleMouseOutsideOfPlaying(x, y)
    if gameState == "menu" then
        handleMouseInMenu(x, y)
    elseif gameState == "difficulty" then
        handleMouseInDifficulty(x, y)
    elseif gameState == "instructions" then
        handleMouseInInstructions(x, y)
    end
end
Also you can replace most comments with meaningful named variables, constants or function names - e.g. leftMouseButton.

Edit:
Don't double check your conditions and the code gets even easier:

Code: Select all

function love.mousepressed(x, y, button)
    local leftMouseButton = 1
    if button ~= leftMouseButton then
        return
    end

    if gameState == "menu" then
        handleMouseInMenu(x, y)
    elseif gameState == "difficulty" then
        handleMouseInDifficulty(x, y)
    elseif gameState == "instructions" then
        handleMouseInInstructions(x, y)
    end
end
Additionally you can use guard clauses and early returns for fewer indentations and better readability.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: fixed

Post by zorg »

Also for the love of everything, do not blank your original post or edit the topic to "FIXED", that's quite disrespectful to anyone that would have learned something from whatever it was.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests