[Solved] Technique for different modes?

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
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

[Solved] Technique for different modes?

Post 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!
Last edited by Eglaios on Thu Jul 22, 2021 9:03 pm, edited 1 time in total.
Currently working on game music...
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: Technique for different modes?

Post 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.
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Technique for different modes?

Post 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%()
Currently working on game music...
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Technique for different modes?

Post 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
  
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Technique for different modes?

Post 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.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Technique for different modes?

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: Technique for different modes?

Post 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.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Technique for different modes?

Post 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. :D
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Technique for different modes?

Post 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!
Currently working on game music...
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests