stateful.lua

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
headchant
Party member
Posts: 105
Joined: Fri Sep 03, 2010 12:39 pm
Contact:

Re: stateful.lua

Post by headchant »

Do you plan on making this compatible with class commons?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: stateful.lua

Post by kikito »

Well, I confess that has never occurred to me. Middleclass is already compatible with it. Does that answer your question?
When I write def I mean function.
User avatar
clickrush
Citizen
Posts: 83
Joined: Tue Dec 13, 2011 12:50 am

Re: stateful.lua

Post 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
Sry about my english.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: stateful.lua

Post 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. :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: stateful.lua

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: stateful.lua

Post 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.
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: stateful.lua

Post by bartbes »

It's what I expected, of course, but <annoying quote about trying anyway here>.
User avatar
headchant
Party member
Posts: 105
Joined: Fri Sep 03, 2010 12:39 pm
Contact:

Re: stateful.lua

Post 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.)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: stateful.lua

Post 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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: stateful.lua

Post by Jack5500 »

I don't really get how my game structure (update,load,draw) fits into the game.lua. Those are just functions.
Post Reply

Who is online

Users browsing this forum: No registered users and 198 guests