3

My friend and I have been working on a game in love2d recently, but in the early stages of developing my computer hard drive stopped working meaning only my friend could work on it. Now I have a computer and I want to make a main menu in Love2d but There is alot of code in the love.load function (generation of the world and such). My question is can I change what is in love.load when the game is running? e.g The main menu loads up, then the generation of the world loads up when the start button is pressed.

Geobits
  • 22,218
  • 6
  • 59
  • 103
Cj1m
  • 805
  • 3
  • 11
  • 26

1 Answers1

4

The love.load function runs only once. As you mention, it's generally used to set up data structures and pre-load other resources. You can use it to handle both the world pre-load and the menu pre-load. Then, control what is active via some sort of state. A simplified example:

local state = "menu"

function love.load()
    preLoadMenu()
    preLoadWorld()
end

function love.update(dt)
    if state == "menu" then
        updateMenu()
    else
        updateWorld()
    end
end

function love.draw()
    if state == "menu" then
        drawMenu()
    else
        drawWorld()
    end
end

function love.mousepressed(x, y, button)   
    if startClicked(x,y,button) then
        state = "world"
    end
end

It's conceivable that you won't want to pre-load absolutely everything for your game on load. Maybe your game is just too big. If that's the case, consider working with an active scene. A scene might be the menu or maybe it's a game level. Again, a simplified example:

local currentScene

function love.load()
    currentScene = loadMenu()
end

function love.update(dt)
    currentScene.update(dt)       
end

function love.draw()
    currentScene.draw()       
end

function love.mousepressed(x, y, button)   
    if startClicked(x,y,button) then            
        currentScene = loadWorld()
    end
end

This second option is much more flexible in the long run. It can handle any number and type of scenes without conditional logic for each. It will require a little "object" thinking. All scenes need a consistent way to update and draw.

If your world takes a while to load, you may want to display a temporary "world is loading" scene so your users don't get impatient.

Corbin March
  • 25,526
  • 6
  • 73
  • 100