Page 1 of 1
[Solved] Technique for different modes?
Posted: Fri Jul 16, 2021 5:49 pm
by Eglaios
Hi!
In a simple game I'm making, I wanted to have different modes : Main menu and battle (how unusual)
To avoid having all the code at the same place, I would have wanted to have different callback functions for each mode, as in the following :
For example, with files "main", "menu", "battle"
Main :
Code: Select all
switch() : Change mode
if mode 1 : mode = require"menu"
if mode 2 : mode = require"battle"
love.update()
mode.update
end
love.draw()
mode.draw
end
Menu :
Code: Select all
update()
%menu stuff%
end
draw()
%menu stuff%
end
Battle :
Code: Select all
update()
%battle stuff%
end
draw()
%battle stuff%
end
I don't really know the syntax for that and didnt really find anything that could explain how to do that, any help appreciated.
I would also be happy to know if there is a better way to do what I want...
Thanks!
Re: Technique for different modes?
Posted: Fri Jul 16, 2021 6:25 pm
by applebappu
My naive approach would be to handle it as a big conditional. Might be slow or clunky but it should work.
set a global variable for game_state = "main", then in your love.update(dt) have something like:
Code: Select all
if game_state == "main" then
main stuff
elseif game_state == "battle" then
battle stuff
blah blah
You could still break out the "stuff" into small methods or put everything into modules and just call them here, however you want to do it.
Re: Technique for different modes?
Posted: Fri Jul 16, 2021 7:03 pm
by Eglaios
applebappu wrote: ↑Fri Jul 16, 2021 6:25 pm
My naive approach would be to handle it as a big conditional. Might be slow or clunky but it should work.
I will probably go this way... Better keep things simple as it's still my first project, I guess... thanks!
One more thing : I saw I could name these functions like "update.menu" and "update.battle"...
Is there anyway to make something like this?
Code: Select all
if mode = 1 -> name = "menu"
if mode = 2 -> name = "battle"
function update.%name%()
Re: Technique for different modes?
Posted: Fri Jul 16, 2021 7:18 pm
by darkfrei
Eglaios wrote: ↑Fri Jul 16, 2021 7:03 pm
applebappu wrote: ↑Fri Jul 16, 2021 6:25 pm
My naive approach would be to handle it as a big conditional. Might be slow or clunky but it should work.
I will probably go this way... Better keep things simple as it's still my first project, I guess... thanks!
One more thing : I saw I could name these functions like "update.menu" and "update.battle"...
Is there anyway to make something like this?
Code: Select all
if mode = 1 -> name = "menu"
if mode = 2 -> name = "battle"
function update.%name%()
Code: Select all
states = {}
states.menu = {
load = menu_load, -- function
update = menu_update,
draw = menu_draw}
states.battle = {
load = battle_load, -- function
update = battle_update,
draw = battle_draw}
state = states.menu -- now state is menu
function love.load()
state.load()
end
function love.update(dt)
state.update(dt)
end
function love.draw()
state.draw()
end
Re: Technique for different modes?
Posted: Fri Jul 16, 2021 9:07 pm
by pgimeno
darkfrei wrote: ↑Fri Jul 16, 2021 7:18 pm
Code: Select all
function love.load()
state.load()
end
Er, nope.
Code: Select all
function love.load(...)
for k, v in pairs(states) do
if v.load then
v.load(...)
end
end
end
love.load is only called once. You don't want to call it for one state, you want to call it for all of them.
Re: Technique for different modes?
Posted: Fri Jul 16, 2021 9:30 pm
by darkfrei
pgimeno wrote: ↑Fri Jul 16, 2021 9:07 pm
love.load is only called once. You don't want to call it for one state, you want to call it for all of them.
Yes, and you can use it as
Code: Select all
if in_menu_condition then
state = states.battle
state.load()
end
Code: Select all
if in_battle_condition then
state = states.menu
state.load()
end
But on program starting will be called the menu one.
The love.load can call other functions, that must be run just once.
Re: Technique for different modes?
Posted: Mon Jul 19, 2021 5:26 pm
by applebappu
Honestly I don't even use love.load() at all. You can just put your initial state variable at the top of your main.lua and it will do exactly the same thing.
Re: Technique for different modes?
Posted: Wed Jul 21, 2021 12:58 pm
by milon
I whipped up a state machine demo a while ago. You can see it here:
https://love2d.org/forums/viewtopic.php?f=14&t=90998
The states are decently well coded, but the overlays (floating GUI bits) not so much. But have a look anyway! I even implemented a quick & dirty fade transition.
Re: Technique for different modes?
Posted: Thu Jul 22, 2021 9:03 pm
by Eglaios
I was away for some time again...
Thank you all to have given new advices meanwhile, I'm sure I'll find what I need there!