I coded something simple to show what I want. There are two state files. StateOne.lua has the user pick one of three stats, each stat has a number to it(Ex.ATTACK stat is 10 and DEFENSE stat is 8). When you pick a stat, you go to StateTwo.lua, which you can make that stat's number bigger or smaller. The code for StateTwo can easily take some parameters from StateOne (like what stat was picked) and work from that.
However the only way I can make this work is if I make each stat have it's own unique StateTwo (like StateTwoAttack or StateTwoDefense) which is a lot of copy and pasting if there are more stats. OR I could put the code from StateTwo into StateOne using a variable state and if statements to switch between the two states, but that can get confusing and error prone. OR use the parameters I do want as a global variables but that doesn't seem efficient either.
Anyways, what do you guys think? Anyone know how to pass parameters into a new gamestate? Is there something I'm missing? The files that need to be looked at are at the root of the folder (main.lua, stateOne.lua, stateTwo.lua). I'll be posting the code for stateOne.lua and stateTwo.lua below too. Any advice or new ways to go about the problem are also welcomed!
stateOne.lua
Code: Select all
local Option_A = require 'objects.obj_option_a'
local TextBox = require 'objects.obj_textBox'
StateOne = {}
function StateOne:init()
end
function StateOne:enter()
self.option = Option_A(10, 16, 20, 12, {"ATTACK", "DEFENSE", "LUCK"})
self.howTo = "Z = Confirm, X = Back"
self.selection = 1 -- WHAT OPTION ARE WE ON NOW? (1 = 'ATTACK', 2 = 'DEFENSE' etc...)
self.explain = {
[1] = "Base attack power",
[2] = "Base defense power",
[3] = "Chance of crit & item drop",
}
self.textBox = TextBox(1, 1, 29, 2)
self.textBox2 = TextBox(1, 5, 29, 2)
end
function StateOne:update(dt)
end
function StateOne:draw()
self.textBox:draw(self.howTo)
self.textBox2:draw(self.explain[self.selection])
self.option:draw()
printStatOnly(hero, "ATTACK", 11, 18)
printStatOnly(hero, "DEFENSE", 11, 20)
printStatOnly(hero, "LUCK", 11, 22)
end
function StateOne:keypressed(key)
if key == 'down' then
if self.selection >= table.getn(self.option.options) then -- if we go out of bounds
self.selection = 1 -- Go to top
else
self.selection = self.selection + 1
end
self.option:setSelect(self.selection)
elseif key == 'up' then
if self.selection <= 1 then -- if we go out of bounds
self.selection = table.getn(self.option.options) -- Go to bottom
else
self.selection = self.selection - 1
end
self.option:setSelect(self.selection)
elseif key == 'z' then
return self:inputHandler(self.selection)
elseif key == 'x' then
end
end
local bindings = {
--WHAT I WANT
--[1] = function() Gamestate.push(StateTwo, "ATTACK", 11, 18) end,
--WHAT WORKS
[1] = function() Gamestate.push(StateTwo) end,
--NOTHING HERE YET
[2] = function() Gamestate.push() end,
[3] = function() Gamestate.push() end,
}
function StateOne:inputHandler(input)
local action = bindings[input]
if action then return action() end
end
function printStatOnly(unit, id, x, y)
local base = unit:GetBase(id)
local full = unit:Get(id)
local space = ""
for i = 1, string.len(id) do
space = space .. " "
end
local str = string.format("%s %d:%d", space, base, full)
love.graphics.print(str, x*8, y*8)
end
Code: Select all
local TextBox = require 'objects.obj_textBox'
StateTwo = {}
function StateTwo:init()
end
--WHAT I WANT
--function StateTwo:enter(stat, x, y)
function StateTwo:enter()
--WHAT I WANT
--[[
self.stat = stat
self.x = x
self.y = y
--]]
self.howTo = "Arrow key to + & -.X to back"
self.textBox = TextBox(1, 1, 29, 2)
end
function StateTwo:update(dt)
end
function StateTwo:draw()
StateOne:draw()
self.textBox:draw(self.howTo)
love.graphics.setColor(255,201,14)
--WHAT I WANT
--printStatOnly(hero, self.stat, self.x, self.y)
--WHAT WORKS (THE HARD CODING)
printStatOnly(hero, "ATTACK", 11, 18)
love.graphics.setColor(255,255,255)
end
-- THIS IS HARD CODED TOO, BUT EASILY CAN TAKE PARAMETERS
function StateTwo:keypressed(key)
local baseValue = hero:GetBase("ATTACK")
if key == 'up' then
if baseValue + 1 <= hero.statLimit then
baseValue = baseValue + 1
hero:EditBaseStat("ATTACK", baseValue)
end
elseif key == 'down' then
if baseValue - 1 > 0 then
baseValue = baseValue - 1
hero:EditBaseStat("ATTACK", baseValue)
end
elseif key == 'x' then
Gamestate.pop()
end
end