stateful.lua
Re: stateful.lua
Do you plan on making this compatible with class commons?
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: stateful.lua
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.
Re: stateful.lua
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
In other words: your work is again very appreciated. thx
Sry about my english.
Re: stateful.lua
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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: stateful.lua
But with class commons it would work with hump, secs and slither as well, I assume that's why he asked it.kikito wrote:Middleclass is already compatible with it.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: stateful.lua
@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.
@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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: stateful.lua
It's what I expected, of course, but <annoying quote about trying anyway here>.
Re: stateful.lua
Okay, yes thanks for clearing that up.
(Great. Now I'm going to have to make my own stateful.lua, with blackjack and hookers.)
(Great. Now I'm going to have to make my own stateful.lua, with blackjack and hookers.)
Re: stateful.lua
Cross-class stateful, for class systems using the __index metamethod (neither blackjack nor hookers included, sorry):
Use it like this:
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
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
I don't really get how my game structure (update,load,draw) fits into the game.lua. Those are just functions.
Who is online
Users browsing this forum: Amazon [Bot] and 3 guests