How to call a Funktion of a instance of a class

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

How to call a Funktion of a instance of a class

Jonas Spiegel
At First, hi.
I'm new in this mailist, and i allready searched in the archived but i found nothing!
I want to call a funktion of a class, which was created in C++
But toLua, luabind and so can only created classes in LUA. But i can't get access to my classes which i defined in C++.
Is where a way to get access and call a C++ funktion of a class in Lua?
Here as example:
class foo
{
public:
    void call(void);
};
 
foo XY;
 
now in Lua:
XY:call();
 
Is somewhere a way to do this?
 
Cye Jonas
Reply | Threaded
Open this post in threaded view
|

Re: How to call a Funktion of a instance of a class

Eero Pajarre-3
Jonas Spiegel wrote:

At First, hi.
I'm new in this mailist, and i allready searched in the archived but i found nothing!
I want to call a funktion of a class, which was created in C++
But toLua, luabind and so can only created classes in LUA. But i can't get access to my classes which i defined in C++.
Is where a way to get access and call a C++ funktion of a class in Lua?
Here as example:
class foo
{
public:
    void call(void);
};
foo XY; now in Lua:
XY:call();
Is somewhere a way to do this?

At the moment I am doing this without toLua or other utilities:

For each C++ object which is visible on the Lua side I create
a Lua table, which contains a light userdata pointer to the
C++ object. To the table I also attach a metatable, with __index entry.

The __index then accesses C++ functions which I have registered
to the Lua side, and which use the actual C++ methods, like "call()"
in your example.

The Lua table is sort of extra package (directly using userdata pointer
might be enough), but it allows me to add additional data fields to
the object in the Lua side, and I like that.

As the metatable and the registered access functions are shared for
all objects of the specific class, they don't cause extra per object
penalty.

		Eero




Reply | Threaded
Open this post in threaded view
|

Re: How to call a Funktion of a instance of a class

Daniel Wallin-2
In reply to this post by Jonas Spiegel
At 10:31 2003-06-14, you wrote:
At First, hi.
I'm new in this mailist, and i allready searched in the archived but i found nothing!
I want to call a funktion of a class, which was created in C++
But toLua, luabind and so can only created classes in LUA. But i can't get access to my classes which i defined in C++.
Is where a way to get access and call a C++ funktion of a class in Lua?
Here as example:
class foo
{
public:
    void call(void);
};

foo XY;

now in Lua:
XY:call();

Is somewhere a way to do this?

This is exactly what both luabind and tolua does.. In luabind you would do it like this:

module(L)
[
   class_<foo>("foo")
      .def("call" &foo::call)
];

Read the docs at http://luabind.sourceforge.net/..


Daniel Wallin, [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Re: How to call a Funktion of a instance of a class

D Burgess-2
In reply to this post by Jonas Spiegel
I have tried a couple of times to add a new application to
http://www.lua.org/uses-form.html

How do I know if it works?



Reply | Threaded
Open this post in threaded view
|

Re: How to call a Funktion of a instance of a class

Ariel Manzur-2
In reply to this post by Jonas Spiegel
Here's an example of how to do it with tolua (4):

--
tolua_pushusertype(lua_state, (void*)&XY, tolua_tag(lua_state, "foo"));
lua_setglobal(lua_state, "XY");
--

That pushes the object XY to the stack and then creates a global called 'XY' on your luastate, with the correct tag (or you could use it to call a function, or set a table.. all the stuff you'll find on the API section of the manual).

bye..

Ariel.

At 10:31 14/06/2003 +0200, you wrote:
At First, hi.
I'm new in this mailist, and i allready searched in the archived but i found nothing!
I want to call a funktion of a class, which was created in C++
But toLua, luabind and so can only created classes in LUA. But i can't get access to my classes which i defined in C++.
Is where a way to get access and call a C++ funktion of a class in Lua?
Here as example:
class foo
{
public:
    void call(void);
};

foo XY;

now in Lua:
XY:call();

Is somewhere a way to do this?

Cye Jonas

Ariel.
http://Anime.com.ar
snm