Page 1 of 1

Help with states and substates

Posted: Thu Aug 17, 2017 5:11 pm
by g0r1ll4
Hello, after creating a arcanoid clone I wanted to go a step further but I'm having trouble with how to structure my project.

Basically I wan't to make a (simple) Binding of Isaac copy with:


=>A player character with health and different attributes
=>A map composed of different rooms with ennemies inside and special rooms (boss and item room)
=>When the player enter a room he need to kill all the ennemies inside so clear it, if he don't clear it the room and the ennemies inside are reinitialised


So i've been thinking about making each room it's own substate of a big game state but I don't know how to implement this and if it's a good way of doing it. Thanks


Image

Re: Help with states and substates

Posted: Sun Aug 20, 2017 11:57 pm
by Zorochase
"Substates" really aren't necessary IMO. I think you're trying to use more layers than you need. You could make each state a table with load, update and draw methods, as well as an ID. You could make another table, with a table inside it to store all states, and a master flag that determines which state is to be loaded, updated, and drawn, like so:

Code: Select all

	Master = {states = {-- All states are inserted here}}
	Master.current = Example.ID
	
	function Master:addState(S) -- 'S' is the state you want to add to the master
		if S.ID then -- Ensure the state has an ID
			table.insert(Master.states, S)
			S:load() -- If this was plugged into main, states may conflict with one another
		else
			return end
	end
	-- -- --
	function love.update(dt)
		for i,v in pairs(Master.states) do -- Find the current state and call it's update method (do the same for love.draw)
			if Master.current = v.ID then
				v:update()
			end
		end
	end
There's a lot more you could do with this idea, this is just the basic premise. Hope this helped!

EDIT: just to clarify, you could do anything within the load, update, and draw methods. If you needed to call any other functions, you could make those calls in the methods for the state(s) you needed them. That could be loading levels, drawing a menu, etc..