jordanvrtanoski / luacpp

Lua C++ wrapper, an easy way to add lua scripting capabilities to an existing C++ project.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: How to invoke lua function after ctx.CompileString

raytangRT opened this issue · comments

LuaContext ctx;
ctx.CompileString("condition", R""""(
       function condition(a, b)
           print('The fastest way to start using lua in a project');
           print(a, b)
           return a or b;
        end
)"""");
///  rule engine trigger when some condition met, 
ctx.AddGlobalVariable("a", std::make_shared<Engine::LuaTNumber>(10));
ctx.AddGlobalVariable("b", std::make_shared<Engine::LuaTNumber>(11));
ctx.Run("condition");            

Hi all,
I am trying to use this lib to create a C++ rule engine, and the user can use Lua functions to define rules.
so here, I want to compile a lua expression, and I expect to call this function when the rule engine calls ctx.Run,

My question is that how to get ctx.Run to execute the function and get the result?

I am happy that you are intending to use the library to build a rule engine. The LuaCPP was build with intent to support and simplify this specific use case.

To receive results from the execution you need to establish a convention for the Lua developer to use global variables to return the results. The Lua developer will need to assure his rule will modify the global variable (and place the return result there for the C++ code to pick it up after execution of the script. That particular case is presented in the example code in TestLuaContext.cpp in the unit tests TestGlobalVariables and TestEnvironmentVariables.

As I mentioned, the intent for LuaCPP was to support creation of rule engines, so the LuaEnvironment was designed with intent to simplify the management of the global variables. All you need to do is to register all your input and output variables in the environment and use the environment to run the lua scripts. LuaCPP will take care of the rest. This will allow you to transfer states between different rule execution, so you can even build a chain of rules.

Assuming that the rule will return number type, your example would look like:

LuaContext ctx;
// Create the global variable to hold result from lua execution
std::shared_ptr<Engine::LuaTNumber> num = std::make_shared<Engine::LuaTNumber>(0);

// Register the global variable
ctx.AddGlobalVariable("result", num)

ctx.CompileString("condition", R""""(
       function condition(a, b)
           print('The fastest way to start using lua in a project');
           print(a, b)
           return a or b;
        end
        result = condition(a, b)
)"""");

///  rule engine trigger when some condition met, 
ctx.AddGlobalVariable("a", std::make_shared<Engine::LuaTNumber>(10));
ctx.AddGlobalVariable("b", std::make_shared<Engine::LuaTNumber>(11));
ctx.Run("condition");     

std::cout << "Value from lua: " << num->getValue() << std::endl;

@jordanvrtanoski Thanks for your prompt reply, 👍