Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
[Android plugin] How to call lua function from Java or C — Gideros Forum

[Android plugin] How to call lua function from Java or C

RickyngkRickyngk Member
edited October 2012 in General questions
Dear all,

Have you ever call a lua function from Java or C, something like invoking a lua-event. Need help !

Regards,

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited October 2012 Accepted Answer
    Well theoretically it's something like:
    //get lua function
    lua_getglobal(L, "name_of_lua_function");
    //pass parameters to function if any
    lua_pushstring(s);
    lua_pushstring(s);
    //call lua function
    //do the call (2 arguments, 1 result)
    lua_pcall(L, 2, 1, 0);
    //get result of function if any
    z = lua_tonumber(L, -1);
    //pop the returned result
    lua_pop(L, 1);
  • In Java, I need to send a phone event to gideros. But, I did not have "L" at this time. How can I get it?
  • @Rickyngk I found sth like wrapper from JAVA to Lua. And as I see "CommonDataKinds_Phone" object is wrapped:
    http://code.google.com/p/wrapandroid-for-multilanguage/
    But it's for plain lua, unfortunately not for Gideros compiled lua files, I think.
  • So about Java part, I think you need to define the existence of native method in java, and then define this method in C.
    Here's an easy example how to do that:
    http://stuf.ro/calling-c-code-from-java-using-jni/

    And about calling lua event, then you can check an example from GoogleBilling Android plugin
    File:
    googlebillingbinder.cpp
    Line: 164 void dispatchEvent(int type, void *event)

    this method gets dispatch event object, creates new event object, dispatches event with values and handles response.
  • RickyngkRickyngk Member
    edited October 2012
    I'm digging google billing plug-in. However, if someone has a simpler example, please support me!

    Thank you in advance!
  • Done, I've coded an init function in C, which called by Lua, for saving lua_state L once. After that, use the same way as @ar2rsawseen suggestion
Sign In or Register to comment.