sonoro1234 / LuaJIT-ImGui

LuaJIT ffi binding for imgui, backends and extension widgets
MIT License
225 stars 29 forks source link

Help with lua and c++. #22

Closed GeTechG closed 3 years ago

GeTechG commented 3 years ago

Sorry to write here, but it is not possible to contact you via github. I am making a c++ application with imgui and a couple more add-ons. And I want to make a scripting for the application. So there will be some folder from which my application takes the scripts. So I want to ask how to do it. So far I have stopped at using sol bindings. I haven't really figured it out yet, but as far as I know, you have to manually specify what you can call from under lua, and it will be difficult to add all the methods manually. Is it possible to combine your work in any way?

sonoro1234 commented 3 years ago

you have to manually specify what you can call from under lua, and it will be difficult to add all the methods manually

This is what you were doing with sol previously, isn`t it?

Is it possible to combine your work in any way?

Which work? Do you mean cimgui?

GeTechG commented 3 years ago

Yes, most likely with cimgui. The thing is, I don't know if I can do it. Because, actually, I need to call the methods that are in my application, and cimgui is a dll library from which methods are executed in lua. I did not work with Sol, I looked at its documentation and did not see there that it is possible to automate what can be called in lua. Here is an example of how this is done:

struct some_class {
    int variable = 30;

    double member_function() {
        return 24.5;
    }
};

int main(int, char*[]) {
    std::cout << "=== functions (all) ===" << std::endl;

    sol::state lua;
    lua.open_libraries(sol::lib::base);

    // put an instance of "some_class" into lua
    // (we'll go into more detail about this later
    // just know here that it works and is
    // put into lua as a userdata
    lua.set("sc", some_class());

    // binds just the member function
    lua["m1"] = &some_class::member_function;

    // binds the class to the type
    lua.set_function("m2", &some_class::member_function, some_class{});

    // binds just the member variable as a function
    lua["v1"] = &some_class::variable;

    // binds class with member variable as function
    lua.set_function("v2", &some_class::variable, some_class{});
}
         -- need class instance if you don't bind it with the function
    print(m1(sc)) -- 24.5
    -- does not need class instance: was bound to lua with one 
    print(m2()) -- 24.5

    -- need class instance if you 
    -- don't bind it with the function
    print(v1(sc)) -- 30
    -- does not need class instance: 
    -- it was bound with one 
    print(v2()) -- 30

    -- can set, still 
    -- requires instance
    v1(sc, 212)
    -- can set, does not need 
    -- class instance: was bound with one 
    v2(254)

    print(v1(sc)) -- 212
    print(v2()) -- 254

In the example, there is one method, and a variable. But in imgui there are a lot of them and I think it will take a long time to prescribe everything by hand.

sonoro1234 commented 3 years ago

sol does a binding from C++ code to plain Lua module. LuaJIT can load plain Lua modules but also can call C functions inside a DLL if you give it the function signature (with ffi.cdef) cimgui takes C++ code and writes some wrapping C functions that can then be called from LuaJIT.

You could use cimgui to write those wrapping C functions from a header file provided by you. cimgui does not understand any C++ code but only a very simple C++ code so I am not sure if it would succed. You could take as example any of the widgets binding instead of imgui itself as they are simpler.

If cimgui succeds you would still need to do the work of providing LuaJIT with the ffi.cdef definition of the C functions and structures (Althought they can just be cut and pasted from the header file generated by cimgui)