Page 1 of 3
Scenes/Views/Activities question [Answered]
Posted: Fri Feb 15, 2019 8:47 pm
by MightyPancake
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!
Re: Scenes/Views/Activities question
Posted: Fri Feb 15, 2019 9:27 pm
by keharriso
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.
Re: Scenes/Views/Activities question
Posted: Fri Feb 15, 2019 9:38 pm
by steVeRoll
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.
Re: Scenes/Views/Activities question
Posted: Fri Feb 15, 2019 9:44 pm
by MightyPancake
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
Re: Scenes/Views/Activities question
Posted: Fri Feb 15, 2019 10:00 pm
by keharriso
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
Re: Scenes/Views/Activities question
Posted: Fri Feb 15, 2019 10:19 pm
by MightyPancake
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!
Re: Scenes/Views/Activities question [Answered]
Posted: Wed Feb 20, 2019 2:36 pm
by tentus
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
Re: Scenes/Views/Activities question
Posted: Wed Feb 20, 2019 4:01 pm
by zorg
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.
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 pmand 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.
Re: Scenes/Views/Activities question [Answered]
Posted: Wed Feb 20, 2019 5:42 pm
by pgimeno
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.
Re: Scenes/Views/Activities question
Posted: Thu Feb 21, 2019 2:26 pm
by MightyPancake
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.
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 pmand 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!