Scenes/Views/Activities question [Answered]

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.
MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

Scenes/Views/Activities question [Answered]

Post by MightyPancake » Fri Feb 15, 2019 8:47 pm

Hello LÖVE users, I have a question, because I wasn't able to find an answer myself. The question is, does LÖVE provide any kind of scene/view/activity manager? In other words, can I switch between different .lua files, or am I stuck with the main.lua (which is fine, but I would still love [heh, get it ;)] to know it anyway). I hope I'll learn something new!
Last edited by MightyPancake on Fri Feb 15, 2019 10:19 pm, edited 1 time in total.

User avatar
keharriso
Citizen
Posts: 73
Joined: Fri Nov 16, 2012 9:34 pm

Re: Scenes/Views/Activities question

Post by keharriso » Fri Feb 15, 2019 9:27 pm

I'm not entirely sure what you mean by being "stuck with main.lua", or exactly what a "scene/view/activity manager" looks like to you. If you're looking for a way to swap out your love.update/love.draw/etc. functions, there's no magic here. Just make a table of the functions you want to swap out and change the table to change game states.

Code: Select all

local state

-- Swap states on mouse press
local stateA = {update = ..., draw = ..., mousepressed = function () state = stateB end}
local stateB = {update = ..., draw = ..., mousepressed = function () state = stateA end}

local state

function love.load()
	state = stateA
end

function love.update(dt)
	state:update(dt)
end

function love.draw()
	state:draw()
end

function love.mousepressed()
	state:mousepressed()
end
Let me know if I misunderstood your question.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games

steVeRoll
Party member
Posts: 102
Joined: Sun Feb 14, 2016 1:13 pm

Re: Scenes/Views/Activities question

Post by steVeRoll » Fri Feb 15, 2019 9:38 pm

Different files do not necessarily mean different states. You can use different files for whatever you want, which in this case is different states. You can use the example keharriso wrote above for what you want.

MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

Re: Scenes/Views/Activities question

Post by MightyPancake » Fri Feb 15, 2019 9:44 pm

keharriso wrote:
Fri Feb 15, 2019 9:27 pm
I'm not entirely sure what you mean by being "stuck with main.lua", or exactly what a "scene/view/activity manager" looks like to you. If you're looking for a way to swap out your love.update/love.draw/etc. functions, there's no magic here. Just make a table of the functions you want to swap out and change the table to change game states.

Code: Select all

local state

-- Swap states on mouse press
local stateA = {update = ..., draw = ..., mousepressed = function () state = stateB end}
local stateB = {update = ..., draw = ..., mousepressed = function () state = stateA end}

local state

function love.load()
	state = stateA
end

function love.update(dt)
	state:update(dt)
end

function love.draw()
	state:draw()
end

function love.mousepressed()
	state:mousepressed()
end
Let me know if I misunderstood your question.
That's exactly what I was asking, but the thing is... I already know this way, it's straight forward BUT it uses a lot of space in editor, is inconvenient and most importantly, it's not ergonomic, the code will run slower etc. What I'm looking for is a way to navigate LÖVE to take code from other file instead of main.lua
Let me give You an example. Imagine creating a game that has a menu. The menu looks of course different then the game itself. So, You create two separate .lua files to manage both menu and game itself. Like Corona's composer, Android Views etc.
So, the point is, how to make LÖVE engine take on other .lua files.
You kind of got the point of what it's used for, however, not the whole idea

User avatar
keharriso
Citizen
Posts: 73
Joined: Fri Nov 16, 2012 9:34 pm

Re: Scenes/Views/Activities question

Post by keharriso » Fri Feb 15, 2019 10:00 pm

OK, how's this:

Code: Select all

-- state.lua
local state = nil
local states = {}

local State = {
	set = function (name) state = states[name] end,
	get = function () return state end
}

local function register(name, state)
	states[name] = state(State)
end

register("stateA", require "stateA")
register("stateB", require "stateB")

return State

Code: Select all

-- stateA.lua
return function (state)
	return {
		update = function (dt) ... end,
		draw = function () ... end,
		mousepressed = function () state.set "stateB" end
	}
end

Code: Select all

-- stateB.lua
return function (state)
	return {
		update = function (dt) ... end,
		draw = function () ... end,
		mousepressed = function () state.set "stateA" end
	}
end

Code: Select all

-- main.lua
state = require "state"

function love.load()
	state.set "stateA"
end

function love.update(dt)
	state.get():update(dt)
end

function love.draw()
	state.get():draw()
end

function love.mousepressed()
	state.get():mousepressed()
end
EDIT: fixed bugs in the example code
Last edited by keharriso on Sat Feb 16, 2019 12:13 am, edited 4 times in total.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games

MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

Re: Scenes/Views/Activities question

Post by MightyPancake » Fri Feb 15, 2019 10:19 pm

keharriso wrote:
Fri Feb 15, 2019 10:00 pm
OK, how's this:

Code: Select all

-- state.lua
local state = nil
local states = {}

local function register(name, state)
	states[name] = state
end

register("stateA", require "stateA")
register("stateB", require "stateB")

return {
	set = function (name) state = states[name] end,
	get = function () return state end
}

Code: Select all

-- stateA.lua
return {
	update = function (dt) ... end,
	draw = function () ... end,
	mousepressed = function () state.set "stateB" end
}

Code: Select all

-- stateB.lua
return {
	update = function (dt) ... end,
	draw = function () ... end,
	mousepressed = function () state.set "stateA" end
}

Code: Select all

-- main.lua
local state = require "state"

function love.load()
	state.set "stateA"
end

function love.update(dt)
	state.get():update(dt)
end

function love.draw()
	state.get():draw()
end

function love.mousepressed()
	state.get():mousepressed()
end
Exactly! Thanks a lot C; That's exactly what I asked for. Have a great night/day!

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Scenes/Views/Activities question [Answered]

Post by tentus » Wed Feb 20, 2019 2:36 pm

There's also an excellent library that might be exactly what you want called gamestate, in the hump libraries.

https://hump.readthedocs.io/en/latest/gamestate.html
Kurosuke needs beta testers

User avatar
zorg
Party member
Posts: 2700
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Scenes/Views/Activities question

Post by zorg » Wed Feb 20, 2019 4:01 pm

MightyPancake wrote:
Fri Feb 15, 2019 9:44 pm
That's exactly what I was asking, but the thing is... I already know this way, it's straight forward BUT it uses a lot of space in editor,
Can't debate this since i'm, for one, not using an IDE.
MightyPancake wrote:
Fri Feb 15, 2019 9:44 pm
is inconvenient
Subjective, which, again is totally a fair point; if you find another solution to be more convenient (with or without any trade-offs), then by all means, use that.
MightyPancake wrote:
Fri Feb 15, 2019 9:44 pm
and most importantly, it's not ergonomic, the code will run slower etc.
Ergonomic in what sense though? If you mean convenience, you already tackled it with "inconvenient";

But this last one, the speed of the code being slower... without benchmarks, that you didn't really provide, it's not really a valid point in and of itself.
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.

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

Re: Scenes/Views/Activities question [Answered]

Post by pgimeno » Wed Feb 20, 2019 5:42 pm

I think he was talking about having all states in one single file. And I agree it uses a lot of space in an editor and is inconvenient and not ergonomic. The speed point is indeed questionable.

MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

Re: Scenes/Views/Activities question

Post by MightyPancake » Thu Feb 21, 2019 2:26 pm

zorg wrote:
Wed Feb 20, 2019 4:01 pm
MightyPancake wrote:
Fri Feb 15, 2019 9:44 pm
That's exactly what I was asking, but the thing is... I already know this way, it's straight forward BUT it uses a lot of space in editor,
Can't debate this since i'm, for one, not using an IDE.
MightyPancake wrote:
Fri Feb 15, 2019 9:44 pm
is inconvenient
Subjective, which, again is totally a fair point; if you find another solution to be more convenient (with or without any trade-offs), then by all means, use that.
MightyPancake wrote:
Fri Feb 15, 2019 9:44 pm
and most importantly, it's not ergonomic, the code will run slower etc.
Ergonomic in what sense though? If you mean convenience, you already tackled it with "inconvenient";

But this last one, the speed of the code being slower... without benchmarks, that you didn't really provide, it's not really a valid point in and of itself.
I'm not here to debate with You, I've written this post to get my question answered. Also, I don't give a quack about benchmarks, it's inconvenient as hell and if You code for sometime, it should be pretty obvious for ya.
Again, ergonomic means I don't want to work with huge files that contain all the data, while I could work with small ones. It's just hard to work with huge files using text editors

The thing that I really, really don't understand about Your "reply" is it's point. As I said, I already have found answer and Your reply didn't help me in any way
Just please, avoid sending replies that have no intellectual value, specially if thread has an answer


Edit: Sorry, zorg, I didn't mean to be rude. Don't feel offended by any way! I hope we're good
If You feel somehow attacked, sorry again!
Last edited by MightyPancake on Thu Feb 21, 2019 4:56 pm, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests