Re: stateful.lua
Posted: Thu Apr 24, 2014 1:09 pm
Thanks. I will not be able to check it today, but I will try to do it tomorrow.
Hey!Then Game can be defined as a class inside Game.lua, and have a Menu state, a Play state, and so on.
Here's how you would create a Game with a Menu state:
That's the basic structure. You could add another state called "Play" similar to "Menu" following this schema.Code: Select all
-- game.lua Game = class("Game"):include(Stateful) function Game:initialize() self.image = love.graphics.newImage("image.jpg") self:gotoState("Menu") -- start on the Menu state end local Menu = Game:addState("Menu") function Menu:enteredState() -- create buttons, options, etc and store them into self print("entering the state menu") end function Menu:draw() -- draw the menu end function Menu:update(dt) -- update anything that needs updates end function Menu:exitedState() -- destroy buttons, options etc here print("exiting the menu state") end
That's pretty much it. Let me know if you have any questions.
Hi there! It took me a while to get some time to test this, with Ludum Dare and Real Life getting in the way.enygmata wrote:I attached one simple love file for testing purposes. To make the problem happen, open the menu.lua file and save it again. Lurker will reload the file and an assertion error will be thrown:kikito wrote:Does any of you guys have a sample *.love file showing the issue?State Menu already exists on class Game
Code: Select all
Game = class('Game')
...
Code: Select all
class = require "middleclass"
lurker = require "lurker"
...
Code: Select all
local class = require 'class'
local Game = class('Game')
... -- (Define Game and Menu)
return Game
Code: Select all
local lurker = require 'lurker'
local Game = require 'Game'
local game
function love.load()
game = Game:new()
end
...
I recommend doing this instead:
Code: Select all
local class = require 'middleclass'
local Stateful = require 'stateful'
Code: Select all
function Troop:initialize() end
function Visible:enteredState() str = str .. '3' end
function love.load()
troop = Troop()
str = '1'
end
function love.draw() lg.print(str,20,20) end
function love.keypressed(key)
if key=='f' then troop:gotoState('Visible') end
if key=='a' then troop:gotoState(nil) end
end
Code: Select all
function Troop:enteredState() str = str .. '2' end
A bit of a slow response, but just wanted to clarify on this point: Unless you meant this was due to the combination of Lurker and Stateful, Lurker should work quite happily with modules which are set to global variables. I wrote it in a way such that it rebuilds all the corresponding module tables in place when it reloads a module, rather than recreating the module. This was so that things like metatables are also immediately effected without objects needing to be reconstructed, and is also the reason for another point you mentioned:kikito wrote:That will give you trouble later on, especially if you want to use it with a dynamic loader such as lurker
I won't go into too much detail, but many of the decisions were made to try and make lurker work really well for quick, small, iterative changes -- for example, if you want to perfectly tweak the gravity in a game and see the effects of this immediately. Due to how it works it does mean you either need to explicitly set a table's deleted function to nil or simply restart the game for changes where you delete a function.kikito wrote: if you remove one function from a state and the state is reloaded by lurker, the function will "still be there" when lurker reloads.
Yes - reason below.CaptainMaelstrom wrote: Running that code and pressing 'f' twice yields a string '133'. Is this intended?
'nil' is not a state. In consequence, it does not have entered/exited callbacks, nor it does occupy a place in the internal state stack.CaptainMaelstrom wrote: A bigger problem for me is that
adding that code and switching to the nil state doesn't add '2' to the string. Is there anyway to get a callback function to trigger when a troop instance goes to the nil state?Code: Select all
function Troop:enteredState() str = str .. '2' end
Code: Select all
function Troop:initialize()
self:gotoState('Default')
end
function Default:enteredState()
str = str .. '1'
end
Thank you for your clarifications.rxi wrote:...