Page 2 of 9

Re: stateful.lua

Posted: Fri Dec 23, 2011 4:53 pm
by headchant
Do you plan on making this compatible with class commons?

Re: stateful.lua

Posted: Fri Dec 23, 2011 5:27 pm
by kikito
Well, I confess that has never occurred to me. Middleclass is already compatible with it. Does that answer your question?

Re: stateful.lua

Posted: Fri Dec 23, 2011 8:28 pm
by clickrush
it is funny that this is one of the main reasons why I started to tell myself: "ok now my code gets really messy and it will be awful to change anything, I should probably do some research on how to organise it"

In other words: your work is again very appreciated. thx

Re: stateful.lua

Posted: Fri Dec 23, 2011 8:37 pm
by coffee
Nice work kikito. Seem very well done. I only will not use it for now cause I really need to know how to do things first. :)

Re: stateful.lua

Posted: Fri Dec 23, 2011 8:41 pm
by bartbes
kikito wrote:Middleclass is already compatible with it.
But with class commons it would work with hump, secs and slither as well, I assume that's why he asked it.

Re: stateful.lua

Posted: Sat Dec 24, 2011 12:22 am
by kikito
@clickrush, @coffee: Thanks for your kind words ^^

@bartbes, @headchant: Before I answered from the train. I've investigated this a bit more, and I don't think I can make Stateful compatible Class-commons. Stateful depends completely on the implementation details of middleclass: things like the way methods are defined and looked up, the difference between class methods and instance methods, and so on. Class-commons, being the "greatest common denominator of all the OOP libs", doesn't even have some of the concepts Stateful "takes for granted" - for example, mixins.

I could be mistaken, but I it sounds either impossible or simply not practical.

Re: stateful.lua

Posted: Sat Dec 24, 2011 12:46 am
by bartbes
It's what I expected, of course, but <annoying quote about trying anyway here>.

Re: stateful.lua

Posted: Sat Dec 24, 2011 9:55 am
by headchant
Okay, yes thanks for clearing that up.

(Great. Now I'm going to have to make my own stateful.lua, with blackjack and hookers.)

Re: stateful.lua

Posted: Sun Dec 25, 2011 2:26 pm
by vrld
Cross-class stateful, for class systems using the __index metamethod (neither blackjack nor hookers included, sorry):

Code: Select all

local function _null_() end

local function goto(self, state, ...)
    if self.state then (self.state._leave or _null_)(self, ...) end
    self.state = state and self.states[state] or {}
    return (self.state._enter or _null_)(self, ...)
end

local function stateful_index(class)
    return function(self, key)
        local st = rawget(self, 'state') or {}
        return (st[key] or class[key])
    end
end

local function add_state(class, name, methods)
    assert(class.states, "Class is not stateful")
    class.states[name] = methods
    return class
end

local function make_stateful(class, methods)
    class.states = {}
    class.__index = stateful_index(class)
    class.goto = goto
    class.add_state = add_state
    return class
end

return make_stateful
Use it like this:

Code: Select all

make_stateful = require 'stateful'

local Enemy = class('Enemy')
make_stateful(Enemy)

function Enemy:initialize(health) self.health = health end
function Enemy:speak() print('My health is ' .. tostring(self.health)) end
Enemy:add_state('Immortal', {
    _enter = function() print('I became Immortal!') end,
    speak = function() print('I am UNBREAKABLE!!') end,
    die = function() print('I can not die now!') end,
    _leave = function() print('The gods have forsaken me') end,
})
-- alternatives:
--    local Immortal = {}
--    function Immortal:speak() (...) end
--    (...)
--    Enemy:add_state('Immortal', Immortal)
-- or:
--    Enemy.states.Immortal = { ... }
-- or even (you get the idea):
--    Enemy.states.Immortal = Immortal

local peter = Enemy:new(10)

peter:speak() --> My health is 10
peter:goto('Immortal') --> I became Immortal~
peter:speak() --> I am UNBREAKABLE!!
peter:die() --> I can not die now!
peter:goto(nil) --> The gods have forsaken me
peter:speak() -- My health is 10

Re: stateful.lua

Posted: Sun Dec 25, 2011 4:37 pm
by Jack5500
I don't really get how my game structure (update,load,draw) fits into the game.lua. Those are just functions.