How to program states inside a gamestate

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
asdf1234
Prole
Posts: 3
Joined: Thu Mar 01, 2018 3:33 pm

How to program states inside a gamestate

Post by asdf1234 » Tue Mar 06, 2018 8:12 pm

Sorry, the title is kind of misleading.. I'm already using hump.gamestates and it's working nicely so far. But I want to progam hmm states inside a gamestate. For example, let's say I wish to show 3 images as a kind of a comic telling a story before the playable character appears. :P

Code: Select all

-- 'somegamestate' room (gamestate)
local somegamestate = Gamestate.new()


-- Love stuff
function somegamestate:enter()
  -- Load assets
end -- somegamestate:new()

function somegamestate:draw()
  intro:draw() -- comic showing a story

  -- remove everything from intro:draw()
  -- before drawing this..
  start:draw()
end -- somegamestate:draw()


-- Additional stuff
function intro()
  -- Show a story like a comic
  love.graphics.circle('fill', W/2, H/2, 50)
end

function start()
  -- game starts here
  -- show character, map, enemies.. everythin
end



return somegamestate

Some help please, I just want some guidance.

lachlaan
Prole
Posts: 30
Joined: Sun Jun 30, 2013 7:23 pm

Re: How to program states inside a gamestate

Post by lachlaan » Tue Mar 06, 2018 8:24 pm

At the most basic level it'd be a couple of ifs with a switch variable, and a trigger within each mini state that allows you to switch states by changing the variable.

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

Re: How to program states inside a gamestate

Post by pgimeno » Tue Mar 06, 2018 11:30 pm

There are many possible solutions. One other solution is to make the "sub-states" be full-fledged states, e.g. game, game2, game3, game4. There are also state libraries that handle the states as a stack, so you can suspend the current state while another one is executing, and resume it when that one finishes. You could perhaps make use of hump.timer's timer.script() function instead, but you need to understand its limitations in order to decide whether it fits your needs. Those are some that come to mind.

Handling them manually as suggested by lachlaan is probably the simplest solution, though.

asdf1234
Prole
Posts: 3
Joined: Thu Mar 01, 2018 3:33 pm

Re: How to program states inside a gamestate

Post by asdf1234 » Wed Mar 07, 2018 1:09 am

@lachlaan, yup when you said switch I got and idea hope it works
@pgimeno, I thought about full-fledged states too but dunno. Maybe I'll try to do something manually with switches.. if that doesn't work then 'sub-states' it is.

Thank you both!

User avatar
jojomickymack
Prole
Posts: 45
Joined: Tue Dec 26, 2017 4:52 pm

Re: How to program states inside a gamestate

Post by jojomickymack » Fri Mar 09, 2018 2:10 pm

I wanted to add that the problem you're describing is a use case for some of the 'state machine' design patterns. Robert Nystrom's 'Game Programming Patterns' book has a chapter describing them.

http://gameprogrammingpatterns.com/state.html

Basically, conditionals and switch statements will solve a lot of problems, but turn into an absolute nightmare given a certain level of complexity. The 'state pattern' recommends taking all that logic and putting into classes representing each state and have some sort of 'state manager'.

Your use case, taken to an extreme where state logic has a lot of 'substates' might necessitate some sort of 'hierarchical state machine'. This website has some good examples/diagrams of what use cases are good for that.

http://jessewarden.com/2012/07/finite-s ... pment.html

Probably bringing these up is in the context of this simple question is a little overkill, but it's good to have some idea that these patterns exist earlier rather than later.

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests