lua-users home
lua-l archive

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


Hi Guy,

 I had a similar problem long time ago, but I wanted to be able to
"inherit" from lua C++ classes (just one more step), I finally did
something like that in "Simple Lua Binder" (SLB), it's called Hybrids.
Basically you start by declaring a class which is the C++ interface:

class HClass : public SLB::Hybrid<HClass>
        {
        public:
                HClass();

                /* int get() { return LCall<int>("get"); } */
                HYBRID_method_0(get,int);

                /* void calc(int a, int b) { return LCall<void, int,
int>("calc"); } */
                HYBRID_method_2(calc,void,int,int);

                HYBRID_const_method_0(update,void);
...

Now from any script in lua you can give the implementation for HClass:
function HClass:calc(a,b)
        print("Calling virtual method HClass:calc")
        if SLB.type(self) ~= "Unit_002::HClass" then
                error("Invalid instance @ calc")
        end
        self:perform_calc(a,b)
end

or create new "types" from HClass:

function HClass.Type1:calc(a,b)
        return a+self:other(b)
end

and so on, for more details at the end of the mail I leave some links
to the hybrid tests.

Regards,
   JL

[SLB]
http://code.google.com/p/slb

[example]
http://code.google.com/p/slb/source/browse/tests/src/unit_002.hpp
http://code.google.com/p/slb/source/browse/tests/src/unit_002.cpp
http://code.google.com/p/slb/source/browse/tests/scripts/test_hybrid_2.lua
http://code.google.com/p/slb/source/browse/tests/scripts/test_hybrid.lua
http://code.google.com/p/slb/source/browse/tests/scripts/test_hybrid_3.lua



On Wed, Sep 7, 2011 at 8:55 AM, Guy J <desfenfe@gmail.com> wrote:
> Hey there Lua fellows!
> First e-mail I'm sending to the list - I hope this is not a repeated
> question, I searched through the archive and couldn't find what I'm looking
> for.
> So.. I've built a game engine in C++ and I have a GameObject class that
> represents all the objects in my game, now I have some other classes that
> derive from GameObject, such as Robot, Bullet, Player, etc...
> What I'd like to do is to extend the GameObject class in Lua instead of
> making C++ derivatives, to keep the engine as clean and simple as possible.
> The Lua object should have awareness of it's own type, and state. For
> instance, the 4 callbacks that will make the object "tick" would be
> Construct, Destruct, Update, Collide -> these will be implemented in Lua.
> These methods should be able to call getLinearVelocity, getPosition,
> applyForce, etc... that will be implemented in the game engine. All of these
> method should work on the context of the same instance.
> What is the best approach for doing what I described?
> Appreciate your help!
> Thanks,
> Guy



-- 
  Jose L. Hidalgo Valiño (PpluX)
  ---- http://www.pplux.com ----