Making different screens

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
galaxified
Prole
Posts: 3
Joined: Sun Aug 07, 2022 4:21 am

Making different screens

Post 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
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Making different screens

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: Making different screens

Post 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

obey
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Making different screens

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

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], MrFariator and 4 guests