Lua oop?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua oop?

Ando Sonenblick
Title: Lua oop?
Gang,

I’m developing an animation engine that will have user level scripting implemented in lua (I’ll be embedding lua into my c++ app).  I’m perusing tons of websites, reading up on the lua.org documentation and so on to learn lua, how to embed it, extend it, etc..  

One thing I am not finding any real info on is any object oriented programming aspects of lua.   As of now, I’m not needing inheritance, encapsulation, etc.  Well, instead of saying what I don’t need, why don’t I simply say what I do need, and hopefully someone can tell me if it’ll be possible:

In my C++ code I have, say, a widget class:

class Widget
{
    long    aValue;

public:

    void    SetValue(long v) { aValue = v; };
    long    GetValue(void) { return aValue; };

};

Now, I want to make this object available for manipulation in a lua script.  So say the script is something like this:

function main(widget)            -- I need first to be able to pass things to a function (but I believe that the stack calls allow this easily enuf)

    widget:SetValue(10);         -- important to get the “:” operator to bind an object reference to a method
    print(widget:GetValue());   -- displays 10

end

So, that’s my initial prime objective: having lua access my C/C++ objects in a clean OOP syntactic way.    I’ve seen some things like tolua and other C++ wrappers, but I’m looking for info on how they do what they do, not just getting a tool to do it.

So, can anyone give me some pointers here?

Thanks much,
Ando
-----------------
SpriTec Software
www.spritec.com

Reply | Threaded
Open this post in threaded view
|

RE: Lua oop?

Benjamin Freidlin
I'm basically in the same situation. I'm only interested in being able to access and deal with my custom C++ class instances from a lua script.
 
Currently I have been exploring LuaPlus, which is a very clean template library that makes embedding Lua pretty much a snap, almost too easy. However, I am still scratching my head as to how I am able to return objects to Lua and enable the Lua script to work with them. 

________________________________

From: [hidden email] on behalf of Ando Sonenblick
Sent: Sat 7/26/2003 5:37 PM
To: [hidden email]
Subject: Lua oop?


Gang,

I'm developing an animation engine that will have user level scripting implemented in lua (I'll be embedding lua into my c++ app).  I'm perusing tons of websites, reading up on the lua.org documentation and so on to learn lua, how to embed it, extend it, etc..  

One thing I am not finding any real info on is any object oriented programming aspects of lua.   As of now, I'm not needing inheritance, encapsulation, etc.  Well, instead of saying what I don't need, why don't I simply say what I do need, and hopefully someone can tell me if it'll be possible:

In my C++ code I have, say, a widget class:

class Widget
{
    long    aValue;

public:

    void    SetValue(long v) { aValue = v; };
    long    GetValue(void) { return aValue; };

};

Now, I want to make this object available for manipulation in a lua script.  So say the script is something like this:

function main(widget)            -- I need first to be able to pass things to a function (but I believe that the stack calls allow this easily enuf)

    widget:SetValue(10);         -- important to get the ":" operator to bind an object reference to a method
    print(widget:GetValue());   -- displays 10

end

So, that's my initial prime objective: having lua access my C/C++ objects in a clean OOP syntactic way.    I've seen some things like tolua and other C++ wrappers, but I'm looking for info on how they do what they do, not just getting a tool to do it.

So, can anyone give me some pointers here?

Thanks much,
Ando
-----------------
SpriTec Software
www.spritec.com


<<winmail.dat>>

Reply | Threaded
Open this post in threaded view
|

RE: Lua oop?

Kevin Baca-2
In reply to this post by Ando Sonenblick
Title: Message
Check this out:
 
 
And especially these:
 
 
-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Ando Sonenblick
Sent: Saturday, July 26, 2003 1:38 PM
To: [hidden email]
Subject: Lua oop?

Gang,

I’m developing an animation engine that will have user level scripting implemented in lua (I’ll be embedding lua into my c++ app).  I’m perusing tons of websites, reading up on the lua.org documentation and so on to learn lua, how to embed it, extend it, etc..  

One thing I am not finding any real info on is any object oriented programming aspects of lua.   As of now, I’m not needing inheritance, encapsulation, etc.  Well, instead of saying what I don’t need, why don’t I simply say what I do need, and hopefully someone can tell me if it’ll be possible:

In my C++ code I have, say, a widget class:

class Widget
{
    long    aValue;

public:

    void    SetValue(long v) { aValue = v; };
    long    GetValue(void) { return aValue; };

};

Now, I want to make this object available for manipulation in a lua script.  So say the script is something like this:

function main(widget)            -- I need first to be able to pass things to a function (but I believe that the stack calls allow this easily enuf)

    widget:SetValue(10);         -- important to get the “:” operator to bind an object reference to a method
    print(widget:GetValue());   -- displays 10

end

So, that’s my initial prime objective: having lua access my C/C++ objects in a clean OOP syntactic way.    I’ve seen some things like tolua and other C++ wrappers, but I’m looking for info on how they do what they do, not just getting a tool to do it.

So, can anyone give me some pointers here?

Thanks much,
Ando
-----------------
SpriTec Software
www.spritec.com

Reply | Threaded
Open this post in threaded view
|

RE: Lua oop?

Daniel Wallin-2
In reply to this post by Ando Sonenblick
At 04:13 2003-07-27, Benjamin Freidlin wrote:
I'm basically in the same situation. I'm only interested in being able to access and deal with my custom C++ class instances from a lua script.

Currently I have been exploring LuaPlus, which is a very clean template library that makes embedding Lua pretty much a snap, almost too easy. However, I am still scratching my head as to how I am able to return objects to Lua and enable the Lua script to work with them.

<shameless plug>
Have you looked at luabind? (luabind.sf.net)
</shameless plug>

---
Daniel Wallin


Reply | Threaded
Open this post in threaded view
|

RE: Lua oop?

Benjamin Freidlin
In reply to this post by Ando Sonenblick
Thanks Daniel,
 
I checked it out and its pretty cool. I have two questions:
 
1) Correct me here but It seems to me that it should be possible to combing LuaPlus and LuaBind at the same time? 
 
One of the things I like about LuaPlus is how it automatically handles creating the state and closing it with its LuaStateOwner class. On the other hand it doesn't seem to offer nearly as many options as LuaBind when it comes to letting Lua access C++ objects and just behave in a more OO way in general. 
 
Since LuaPlus's classes can provide access to the raw lua state pointer and other raw lua.h types, is there any reason why I can't use it to handle the runtime lifetime management and then rely on LuaBind to expose the innerparts of my C++ application?
 
2) I'm confused by the part of your documentation section entitled "Object" that starts out by saying: "Since functions have to be able to take lua values (of variable type) we need a wrapper around them." Just so I don't missunderstand, this is intended for functions we write that we know in advance will be interrop-ing with lua scripts and tables right? Its not required that all the functions I expose accept Object, since I saw plenty of examples in the docs that do not, just wanna be sure here.
 
Thanks, sorry for my verbose question style...
 
Benjamin

________________________________

From: [hidden email] on behalf of Daniel Wallin
Sent: Sun 7/27/2003 2:39 AM
To: Lua list
Subject: RE: Lua oop?



At 04:13 2003-07-27, Benjamin Freidlin wrote:
>I'm basically in the same situation. I'm only interested in being able to
>access and deal with my custom C++ class instances from a lua script.
>
>Currently I have been exploring LuaPlus, which is a very clean template
>library that makes embedding Lua pretty much a snap, almost too easy.
>However, I am still scratching my head as to how I am able to return
>objects to Lua and enable the Lua script to work with them.

<shameless plug>
Have you looked at luabind? (luabind.sf.net)
</shameless plug>

---
Daniel Wallin



<<winmail.dat>>

Reply | Threaded
Open this post in threaded view
|

RE: Lua oop?

Nick Trout
In reply to this post by Ando Sonenblick
If there is anything missing that you would like to see I will quite happily try and add it when I get the time. - There is a feedback page on the wiki so you can make noise.

The tutorial currently focuses on the Lua syntax and not the language C API. Hopefully this will be covered in due course.

Regards,
Nick

P.S. I'm glad people are finding it useful!


-----Original Message-----
From: Kevin Baca [[hidden email]] 
Sent: Saturday, July 26, 2003 7:38 PM
To: 'Lua list'
Subject: RE: Lua oop?

Check this out:
 
http://lua-users.org/wiki/TutorialDirectory
 
And especially these:
 
http://lua-users.org/wiki/ObjectOrientationTutorial
http://lua-users.org/wiki/MetamethodsTutorial