Page 1 of 1

Making different screens

Posted: Sun Aug 07, 2022 10:58 pm
by galaxified
How do I make different screens? Like a title screen, play screen, settings screen, etc.

Since it doesn't all go in main.lua, right? I'm not sure if this is the most efficient way, but my idea was to make a different .lua file for each screen, but I'm not sure where to go from there. The screen happens immediately when I call

Code: Select all

require 'examplefile'
but I can't return

So how do I do this? Or is there a different, "standard" way to do it? If so please let me know, thanks

Re: Making different screens

Posted: Mon Aug 08, 2022 6:44 am
by ReFreezed
There isn't a standard way to do this. You could have everything in one file (main.lua or elsewhere), or use a system with multiple files. Here's one simple way to do it, and the general idea of handling different screens, in half-pseudo code:

Code: Select all

local screen = "mainmenu"

function love.update(dt)
	if screen == "mainmenu" then
		-- Update main menu stuff.
		if someButtonIsPressed() then
			screen = "ingame"
		end

	elseif screen == "ingame" then
		-- Update in-game stuff.
		if playerIsDead() then
			screen = "mainmenu"
		end
	end
end

function love.draw()
	if screen == "mainmenu" then
		-- Draw main menu.

	elseif screen == "ingame" then
		-- Draw in-game stuff.
	end
end
These "screens" are often called scenes, so you may want to look up scene managing libraries, unless you want to roll your own solution.

Re: Making different screens

Posted: Mon Aug 08, 2022 7:38 am
by BrotSagtMist
Yea for the beginning its just fine to stuff it all in the main file.
Instead of having if cases you can also just reassign the function, remember names arent very strict in lua:

Code: Select all

function title()
 lg.print(TITLE)
 if keyboard.IsDown("return") then
  love.update=game
 end
end
function game()
 gamestuff()
end
love.update=title


Re: Making different screens

Posted: Mon Aug 08, 2022 8:57 am
by pgimeno
I covered this topic in depth in this post: viewtopic.php?p=194226#p194226

The basic idea is to have every file have its own events, and call them from main.