lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


That´s good, but I need a more generic solution, because my game will has a lot of classes.

Using the syntax of one solution I´ve seen:


CActor = Class("Actor")

function CActor:update()
   print ("Actor:update")
end

--                Name      Base
CSoldier = Class("Soldier", Actor)

function CSoldier:update()
   self.base:update()
   print ("Soldier:update")
end


soldier1 = CSoldier:new()

soldier1:update()



I will investigate this more later tonight.

Thanks for the reply

Jose



--- Em qua, 1/6/11, Rebel Neurofog <rebelneurofog@gmail.com> escreveu:

> De: Rebel Neurofog <rebelneurofog@gmail.com>
> Assunto: Re: Simple classes
> Para: "Lua mailing list" <lua-l@lists.lua.org>
> Data: Quarta-feira, 1 de Junho de 2011, 7:58
> actor = {}
> function actor.new ()
>    local self = {}
> 
>    -- public:
>    function self.update ()
>       print ("Actor::update")
>    end
> 
>    return self
> end
> 
> 
> soldier = {}
> function soldier.new ()
>    local self = actor.new ()
> 
>    -- private:
>    local actor_update = self.update
> 
>    -- public:
>    function self.update ()
>       actor_update ()
>       print ("Soldier::update")
>    end
> 
>    return self
> end
> 
> local soldier1 = soldier.new ()
> soldier1.update ()
> 
> -------- Jose, is that OO enough?
> -------- If not, here's the trick:
> 
> actor = {}
> local function actor_new ()
>    local self = {}
> 
>    -- public:
>    function self.update ()
>       print("Actor::update")
>    end
> 
>    return self
> end
> setmetatable (actor, {__call = actor_new})
> 
> soldier = {}
> local function soldier_new ()
>    local self = actor ()
> 
>    -- private:
>    local actor_update = self.update
> 
>    -- public:
>    function self.update ()
>       actor_update ()
>       print("Soldier::update")
>    end
> 
>    return self
> end
> setmetatable (soldier, {__call = soldier_new})
> 
> local soldier1 = soldier ()
> soldier1.update ()
> 
>