Using Lua to implement C++ methods.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Lua to implement C++ methods.

Carsten Fuchs
Hello all,

I'm new to Lua, but I think that it is an excellent scripting language
and would like to use it in combination with C++. I've read a good deal
of http://www.lua.org/pil/ , especially "Part IV. The C API", but
apparently due to my lack of experience with Lua, I've not been able to
my problem myself.

The problem is that I have a simple hierarchy (a tree) of C++ objects,
as for example windows that can be found in a GUI system:
(For clarity, I'm just providing pseudo-code, it will probably not compile.)

class Window
{
     public:

     Window* Find(char* n);    // Find a window with name "n" in the
hierarchy tree of this window.

     void Draw();              // Draws this window on the screen -
implemented in C++.
     void SomeMethod(float f); // Some helper method for demonstration
purposes, also implem. in C++.
     void OnMouseClick();      // Some event handler - supposed to be
implemented by a Lua script!

     std::string name;         // Name of this window, e.g. for
reference in Lua scripts.
     int         pos[2];       // The position of the top-left corner of
this window.
     int         size[2];      // The size of this window in pixels.
     float       back_col[3];  // The background color.
     float       text_col[3];  // The text / foreground color.
     std::string text;         // Text to be displayed in the window.

     Window*         parent;   // The parent of this window (may be NULL).
     vector<Window*> children; // The sub-windows of this window (e.g.
the buttons in a dialog).
};


As mentioned in the comments, I'd like to use Lua to implement the
OnMouseClick() method of each Window, so that each Window has an
individual implementation if desired.

Here is an example for an implementation of the OnMouseClick() method as
I'd write it in C++:

void Window::OnMouseClick()
{
     // The contents of this method is supposed to
     // be implemented with a Lua script...

     back_col[0]=0.0f;
     back_col[1]=0.3f;
     back_col[2]=0.9f;

     SomeMethod(0.2f);

     size[0]=100.0f;
     size[1]=20.0f;

     text="I was clicked!";


     WindowT* other=Find("Button OK");

     other->SomeMethod(3.0f);
     other->pos[0]=80.0f;
     other->text="OK";

     parent->text="My parent too...";

     // and so on...
}


So in spite of having read the PIL book, I have not the faintest idea
what the best approach to this problem is:
I might be able to model a table hierachy in Lua that mirrors the Window
hierachy in C++, but what then?
Don't I have to manually "synchronize" both hierarchies after each call
from/to Lua/C++??
Should I use userdata instead, analogous to
http://www.lua.org/pil/28.1.html?

Are there any C++ specific examples where I can learn how this problem
should be approached?

Any help would much be appreciated!
Thank you very much for your time!

Best regards,
Carsten

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua to implement C++ methods.

Noel Frankinet
Carsten Fuchs wrote:

> Hello all,
>
> I'm new to Lua, but I think that it is an excellent scripting language
> and would like to use it in combination with C++. I've read a good
> deal of http://www.lua.org/pil/ , especially "Part IV. The C API", but
> apparently due to my lack of experience with Lua, I've not been able
> to my problem myself.
>
> The problem is that I have a simple hierarchy (a tree) of C++ objects,
> as for example windows that can be found in a GUI system:
> (For clarity, I'm just providing pseudo-code, it will probably not
> compile.)
>
> class Window
> {
>     public:
>
>     Window* Find(char* n);    // Find a window with name "n" in the
> hierarchy tree of this window.
>
>     void Draw();              // Draws this window on the screen -
> implemented in C++.
>     void SomeMethod(float f); // Some helper method for demonstration
> purposes, also implem. in C++.
>     void OnMouseClick();      // Some event handler - supposed to be
> implemented by a Lua script!
>
>     std::string name;         // Name of this window, e.g. for
> reference in Lua scripts.
>     int         pos[2];       // The position of the top-left corner
> of this window.
>     int         size[2];      // The size of this window in pixels.
>     float       back_col[3];  // The background color.
>     float       text_col[3];  // The text / foreground color.
>     std::string text;         // Text to be displayed in the window.
>
>     Window*         parent;   // The parent of this window (may be NULL).
>     vector<Window*> children; // The sub-windows of this window (e.g.
> the buttons in a dialog).
> };
>
>
> As mentioned in the comments, I'd like to use Lua to implement the
> OnMouseClick() method of each Window, so that each Window has an
> individual implementation if desired.
>
> Here is an example for an implementation of the OnMouseClick() method
> as I'd write it in C++:
>
> void Window::OnMouseClick()
> {
>     // The contents of this method is supposed to
>     // be implemented with a Lua script...
>
>     back_col[0]=0.0f;
>     back_col[1]=0.3f;
>     back_col[2]=0.9f;
>
>     SomeMethod(0.2f);
>
>     size[0]=100.0f;
>     size[1]=20.0f;
>
>     text="I was clicked!";
>
>
>     WindowT* other=Find("Button OK");
>
>     other->SomeMethod(3.0f);
>     other->pos[0]=80.0f;
>     other->text="OK";
>
>     parent->text="My parent too...";
>
>     // and so on...
> }
>
>
> So in spite of having read the PIL book, I have not the faintest idea
> what the best approach to this problem is:
> I might be able to model a table hierachy in Lua that mirrors the
> Window hierachy in C++, but what then?
> Don't I have to manually "synchronize" both hierarchies after each
> call from/to Lua/C++??
> Should I use userdata instead, analogous to
> http://www.lua.org/pil/28.1.html?
>
> Are there any C++ specific examples where I can learn how this problem
> should be approached?

Hello,
I am still very new to lua, but I've found that the Lunar template is
very easy to use and keep a "object orientation" on the lua side .
Did you try it ?

>
> Any help would much be appreciated!
> Thank you very much for your time!
>
> Best regards,
> Carsten
>
>
>


--
Noël Frankinet
Gistek Software SA
http://www.gistek.net

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua to implement C++ methods.

mark gossage
In reply to this post by Carsten Fuchs
Hello Carsten,

One possible and usually quite painless method is to consider a tool to do the binding for you. Luabind, tolua++ and SWIG can all produce bindings from C++ - Lua, you could try them as a comparison.
(Personally I use SWIG, but I am also the maintianer for the Lua bindings, so I am biased).

If you want to DIY, this a quite viable under Lua, you will need to use a UserData, the stuff in http://www.lua.org/pil/28.1.html is exactly where you should be looking.
The OnClick() method which is to be implmneted in Lua is probably going to be the hard bit. Can any of the tolua for Luabind guru's out there provide any ideas on this?

Hope this is a useful pointer to you.

Regards,
Mark Gossage

> Hello all,
>
> I'm new to Lua, but I think that it is an excellent scripting language
> and would like to use it in combination with C++. I've read a good deal
> of http://www.lua.org/pil/ ,
> especially &quot;Part IV. The C API&quot;, but
> apparently due to my lack of experience with Lua, I've not been able to
> my problem myself.
>
> The problem is that I have a simple hierarchy (a tree) of C++ objects,
> as for example windows that can be found in a GUI system:
> (For clarity, I'm just providing pseudo-code, it will probably not compile.)
>
> class Window
> {
>      public:
>
>      Window* Find(char* n);    // Find a window with name &quot;n&quot; in the
>
> hierarchy tree of this window.
>
>      void Draw();              // Draws this window on the screen -
> implemented in C++.
>      void SomeMethod(float f); // Some helper method for demonstration
> purposes, also implem. in C++.
>      void OnMouseClick();      // Some event handler - supposed to be
> implemented by a Lua script!
>
>      std::string name;         // Name of this window, e.g. for
> reference in Lua scripts.
>      int         pos[2];       // The position of the top-left corner of
> this window.
>      int         size[2];      // The size of this window in pixels.
>      float       back_col[3];  // The background color.
>      float       text_col[3];  // The text / foreground color.
>      std::string text;         // Text to be displayed in the window.
>
>      Window*         parent;   // The parent of this window (may be NULL).
>      vector&lt;Window*&gt; children; // The sub-windows of this window (e.g.
>
> the buttons in a dialog).
> };
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Using Lua to implement C++ methods.

Noel Frankinet
Hello all,

>>I'm new to Lua, but I think that it is an excellent scripting language
>>and would like to use it in combination with C++. I've read a good deal
>>of http://www.lua.org/pil/ ,
>>especially &quot;Part IV. The C API&quot;, but
>>apparently due to my lack of experience with Lua, I've not been able to
>>my problem myself.
>>
>>The problem is that I have a simple hierarchy (a tree) of C++ objects,
>>as for example windows that can be found in a GUI system:
>>(For clarity, I'm just providing pseudo-code, it will probably not compile.)
>>
>>class Window
>>{
>>     public:
>>
>>     Window* Find(char* n);    // Find a window with name &quot;n&quot; in the
>>
>>hierarchy tree of this window.
>>
>>     void Draw();              // Draws this window on the screen -
>>implemented in C++.
>>     void SomeMethod(float f); // Some helper method for demonstration
>>purposes, also implem. in C++.
>>     void OnMouseClick();      // Some event handler - supposed to be
>>implemented by a Lua script!
>>
>>     std::string name;         // Name of this window, e.g. for
>>reference in Lua scripts.
>>     int         pos[2];       // The position of the top-left corner of
>>this window.
>>     int         size[2];      // The size of this window in pixels.
>>     float       back_col[3];  // The background color.
>>     float       text_col[3];  // The text / foreground color.
>>     std::string text;         // Text to be displayed in the window.
>>
>>     Window*         parent;   // The parent of this window (may be NULL).
>>     vector&lt;Window*&gt; children; // The sub-windows of this window (e.g.
>>
>>the buttons in a dialog).
>>};
>>
>>    
>>
Again, you can use Lunar for the OnClick call-back
here is what I do :
---------------------------------------------------------------------------
void LuaMenu::OnDispatch(void *ptr1,void *ptr2,const string & cmd)
{
      Gui_dialog *d = (Gui_dialog *)ptr1;
      Gui_gadget *g = (Gui_gadget *)ptr2;
      lua_State *L = (lua_State *)d->user_info();
        //find a way to push dialog and gadget on lua stack
      /* push functions and arguments */
      lua_getglobal(L, cmd.c_str());
      LuaDialog dialog(L,d);
      Lunar<LuaDialog>::push(L,&dialog);
      LuaGadget gadget(L,g);
      Lunar<LuaGadget>::push(L,&gadget);


      /* do the call (0 arguments, 0 result) */
      if (lua_pcall(L, 2, 0, 0) !=0)
      {    char buf[500];
        sprintf(buf, "error running function `f': %s",
                 lua_tostring(L, -1));
        MessageBox(NULL,wstr(buf),L"Error",MB_OK);
      }
}
-------------------------------------------------------

In the lua world you end up with a dialog and gadget "object" that you
can use "dialog:method"

Maybe it will help
Best wishes

--
Noël Frankinet
Gistek Software SA
http://www.gistek.net

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua to implement C++ methods.

Carsten Fuchs
Hi all,

Noel and Mark, thank you very much for your replies - they helped a lot!

I'm still digging through the API details, the Lunar template and the
details of my host app, and am still sorting things out, trying to learn
as much and as thoroughly as possible.
This will probably take me yet another few days, certainly also the
weekend, and eventually I'll come back either with success report or
more questions.  ;)  (However, the more I learn about the Lua details,
the more optimistic I get, its mostly a question of code design issues
right now...)

So far, I like the Lunar script a lot, because it is short, relatively
simple (yes, I have a chance to understand everything in it eventually!
;) ), relatively portable, and close to the PIL book. Nonetheless, I'm
continueing to look into the altervatives that Mark has mentioned before
making a descision.

Again, thank you very much for your help!

Best regards,
Carsten



--
Ca3D - Engine    http://www.Ca3D-Engine.de
Carsten Fuchs    http://www.Ca3D-Engine.de/c_Carsten.php
Reply | Threaded
Open this post in threaded view
|

Re: Using Lua to implement C++ methods.

Noel Frankinet
Carsten Fuchs wrote:

> Hi all,
>
> Noel and Mark, thank you very much for your replies - they helped a lot!
>
> I'm still digging through the API details, the Lunar template and the
> details of my host app, and am still sorting things out, trying to
> learn as much and as thoroughly as possible.
> This will probably take me yet another few days, certainly also the
> weekend, and eventually I'll come back either with success report or
> more questions.  ;)  (However, the more I learn about the Lua details,
> the more optimistic I get, its mostly a question of code design issues
> right now...)
>
> So far, I like the Lunar script a lot, because it is short, relatively
> simple (yes, I have a chance to understand everything in it
> eventually! ;) ), relatively portable, and close to the PIL book.
> Nonetheless, I'm continueing to look into the altervatives that Mark
> has mentioned before making a descision.
>
> Again, thank you very much for your help!
>
> Best regards,
> Carsten
>
>
>
Hello Carsten,

You are welcome,

Yes, I like the Lunar template too, small and easy to understand. I
think the other methods (swig like) are more useful when you have a lib
to bind in one pass. Lunar is more for a lib you are developping.

Best wishes.



--
Noël Frankinet
Gistek Software SA
http://www.gistek.net

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua to implement C++ methods.

Carsten Fuchs
In reply to this post by Carsten Fuchs
Hi again,

just for completeness, I forgot to mention that I did not find a link to
the Lunar template when browsing the wiki (or maybe I've been blind); I
only eventually found it via Google after Noel mentioned it.

I found these sites especially useful:
http://lua-users.org/wiki/SimpleCppBinding
http://lua-users.org/wiki/SimplerCppBinding
http://lua-users.org/wiki/CppBindingWithLunar
http://lua-users.org/wiki/CallingLuaFromCpp

Best,
Carsten



--
Ca3D - Engine    http://www.Ca3D-Engine.de
Carsten Fuchs    http://www.Ca3D-Engine.de/c_Carsten.php