Gamestates and "..."

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
User avatar
Lirija
Prole
Posts: 28
Joined: Tue Mar 14, 2017 11:42 am
Contact:

Gamestates and "..."

Post by Lirija »

*** EDIT: Ok, i'm an idiot; i used for k,v in ipairs(table) but the loaded table had only string keys and seems like ipairs ignores them... :cry:

Hi everyone!
Currently i'm trying to make a space shooter but i got stuck;
I have a "game" that manages the main game and i wanted the game.enter() function to load the map only if the previous gamestate was "gameover", but i'm having problems passing the old state name, it keep saying that i'm passing a nil value.
I've tried

Code: Select all

 
function game.enter(...)
     local prev = ...
     if prev == "gameover" then
         map_manager.load("map.space1")
     end
(and several other random things, like adding : and / or a prev_state argument before the ...)


I'm using noooway's gamestates module:

Code: Select all

local gamestates = {}

local current_state = nil 
local loaded = {} 


function gamestates.state_event(function_name, ...)
	if current_state and type(current_state[function_name]) == "function" then 
		current_state[function_name](...) -- sends additional args to game.enter()
	end
end

function gamestates.set_state(state_name, ...)

	gamestates.state_event("exit")
	local old_state_name = get_key_for_value(loaded, current_state) -- get_key_for_value(table, value) return key
	current_state = loaded[ state_name ] 
	if not current_state then 
		current_state = require( "gamestates/" .. state_name )
		loaded[ state_name ] = current_state 
		gamestates.state_event("load", old_state_name, ... ) 
	end
	gamestates.state_event("enter", old_state_name, ... ) 
end

function get_key_for_value(table, value)
	for k,v in ipairs(table) do
		if v == value then
			return k
		end
	end
end

return gamestates
Last edited by Lirija on Mon Feb 26, 2018 10:49 am, edited 2 times in total.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Gamestates and "..."

Post by Beelz »

I do states a little differently than some, but I like how it works... In 'main.lua' I have a table of usable states(which inherit from a base so they have fallback methods predefined) and I create an event for state changes right in Love's event system:

Code: Select all

local curState = "menu"

local States = {
	menu = require("src.state.menu"),
	game = require("src.state.game")
}

function love.handlers.scene(new, ...)
	if not new or not States[new] then return end

	local switchArgs = curState and States[curState]:onExit(new, ...) or nil 	-- send new state and any other args to current state's exit method
	States[new]:onEnter(curState, switchArgs)		-- send previous state and any other args to new state's enter method
	curState = new	-- switch state
end
Then to use it is very simple:

Code: Select all

-- Say we want to start a new game with 5 players and 100 enemies for example
love.event.push("scene", "game", "new", 5, 100)

-- Then inside the "game" state
function state:onEnter(prev, mode, players, enemies)
	if mode == "new" then
		print(string.format("Starting new game with %s players and %s enemies.", players, enemies))
	end
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 232 guests