GithubHelp home page GithubHelp logo

cxlua's Introduction

cxLua

Simple C++ wrapper for Lua VM.

Here is an example using the library:

// Class that will be exposed to the Lua engine
class MyUnit : public Scriptable
{
public:
    MyUnit()
    {
        // Register all methods to be exposed to Lua engine
        registerMethod("sum", static_cast<Method>(&MyUnit::sum));
    }

    // Sum all arguments
    Variant sum(const VariantList &args)
    {
        double res = 0.0;
        for (VariantList::const_iterator it = args.begin(); it != args.end(); ++it) {
            res += (*it).toReal();
        }
        return res;
    }

};

// Function to be exposed to the Lua engine
Variant lua_test(const VariantList &args, void *pData)
{
    (void)pData;    // Not used

    // Print passed arguments
    for (VariantList::const_iterator it = args.begin(); it != args.end(); ++it) {
        std::cout << (*it) << std::endl;
    }

    return Variant();
}

int main()
{
    LuaEngine lua;

    // 1. Evaluate Lua script
    lua.evaluate("function add(x, y) return x + y end");
    if (lua.isError()) {
        std::cerr << "Lua error: " << lua.errorText() << std::endl;
    }

    // 2. Invoke Lua function
    Variant res = lua["add"](2, 3);
    std::cout << "res = " << res << std::endl;

    // 3. Assign Lua global variable
    lua["GlobalValue"] = "test";

    // 4. Read Lua global variable
    std::cout << "GlobalValue = " << lua["GlobalValue"].value() << std::endl;

    // 5. Expose C++ object to Lua
    MyUnit myUnit;
    lua.registerObject("MyUnit", &myUnit);
    lua.evaluate("local s = MyUnit.sum(1, 2, 3, 4, 5)\n"
                 "print('sum = ' .. s)");

    // 6. Expose function to Lua
    lua.registerFunction("test", lua_test);
    lua.evaluate("test('a', 'b', 'c')");

    // 7. Expose lambda-function to Lua
    lua.registerFunction("test2", [](const VariantList &args, void *pData) -> Variant {
                            for (VariantList::const_iterator it = args.begin(); it != args.end(); ++it) {
                                std::cout << (*it) << std::endl;
                            }
                            return Variant();
                         });
    lua.evaluate("test2(1, 2, 3)");

    return 0;
}

cxlua's People

Contributors

archie3d avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.