Help with states and substates

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
g0r1ll4
Prole
Posts: 5
Joined: Mon Aug 07, 2017 3:59 am

Help with states and substates

Post 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
Zorochase
Prole
Posts: 21
Joined: Sun May 21, 2017 10:40 pm
Contact:

Re: Help with states and substates

Post 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..
"I am a tomato. My favorite food is tomatoes. Tomatoes are the best. I eat them everyday. I love to hear them scream."
Post Reply

Who is online

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