How to pass a class arg to lua function

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

How to pass a class arg to lua function

mos-4

Hi!

         I use luabind bind class cChar and there is a lua script function like

 

         function Hello(cChar* p)

                   p->Say(‘Hello’)

         end

 

         ——but how to pass the cChar to Hello in cpp file

 

     cChar* p;

     lua_getglobal(L,Hello);

     lua_pushlightuserdata(L, p);         //there must be wrong

     int error = lua_pcall(L, 1, 0,0);   

 

         then luabind report error.

         What can I do ?

 

Mos

Reply | Threaded
Open this post in threaded view
|

Re: How to pass a class arg to lua function

mos-4

Hi!
        I am very sorry choose a html format prev mail

    I use luabind bind class cChar and it's member function Say
        there is a lua script function like
         function Hello(cChar* p)
                   p:Say('Hello')
         end
     --but how to pass the cChar to lua Function Hello in cpp file

     cChar* p;
     lua_getglobal(L,"Hello");
     lua_pushlightuserdata(L, p);         //there must be wrong.
     int error = lua_pcall(L, 1, 0,0);    

        then luabind report error.
        What can I do ?

Mos




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: How to pass a class arg to lua function

Josh Green-3
Hi, since you havn't got any replies yet and I can give you some quick
pointers, here we go....

Sorry if I'm patronising in this reply as from the looks of it this is your
first go at luabind and you don't understand it completely yet. I don't
claim to know how it all works, but i've got it doing what i want so far.

Anyway, first of all you must "bind" your class into luabind, without doing
this you won't be able to do anything in lua using your classes. to do this
you do something like this:

void bindCharClass(){
    module(luaState)[
       class_<cChar>("cChar")     //Bind my class to luabind and give it a
name
          .def("Say",&cChar::Say)  //tell luabind about the function "say"
which is in the class

    ]
}

Once you have run that function THEN you can perform a script like this:

function Hello(p)
    p:Say("Hello")
end

Notice that the script does NOT have cChar *p in it, this is because lua is
dynamically typed, so p could effectivly be anything, (integer, string, or
even your class).

now to execute that function you would do something like this:

char *fileName = "test.lua";

//First run the script described above:
lua_dofile(lua,fileName);

//Get the function object from lua
luabind::object globals = get_globals(luaState);
luabind::object function = globals["Hello"];

cChar* p;
if(function.type()==LUA_TFUNCTION){
   (function)(p);  //Call the function
}

Now hopefully i've given you some ideas.. either that or i've confused you,
or i'm completely missing the point of your question (in which case i
imagine others will too)

Either way, The documentation on LuaBind can be found at:
http://www.rasterbar.com/products/luabind/docs.html
and the lua documentation can be found at:
http://www.lua.org/manual/

Also note that lua 5.1 doesn't appear to work with luabind yet, and so you
should use the lua 5.0 source when compiling that library

Hope i've been of some help
--Josh




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

答复: [luabind] How to pass a class arg to lua function

mos-4


>Hi, since you havn't got any replies yet and I can give you some quick
>pointers, here we go....
>
>Sorry if I'm patronising in this reply as from the looks of it this is your

>first go at luabind and you don't understand it completely yet. I don't
>claim to know how it all works, but i've got it doing what i want so far.

Hi
        Thanks very much and you gives me exactly the answer I want. And
explain so much than I expected, I feel very warm, indeed.
        I will test it following your email - it is a simple thing now. :)

Mos




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user