/**
* This example demonstrates how to use primitive property types (int, bool, string etc.)
*/
int main()
{
rlogic::LogicEngine logicEngine;
// Create a simple script which multiplies two numbers and stores the result in a string
rlogic::LuaScript* script = logicEngine.createLuaScriptFromSource(R"(
function interface()
IN.param1 = INT
IN.param2 = FLOAT
OUT.result = STRING
end
function run()
OUT.result = "Calculated result is: " .. IN.param1 * IN.param2
end
)", "MultiplyScript");
/**
* Query the inputs of the script. The inputs are
* stored in a "Property" instance and can be used to
* get the information about available inputs and outputs
*/
rlogic::Property* inputs = script->getInputs();
for (size_t i = 0u; i < inputs->getChildCount(); ++i)
{
rlogic::Property* input = inputs->getChild(i);
std::cout << "Input: " << input->getName() << " is of type: " << rlogic::GetLuaPrimitiveTypeName(input->getType()) << std::endl;
}
// We can do the same with the outputs
const rlogic::Property* outputs = script->getOutputs();
for (size_t i = 0u; i < outputs->getChildCount(); ++i)
{
const rlogic::Property* output = outputs->getChild(i);
std::cout << "Output: " << output->getName() << " is of type: " << rlogic::GetLuaPrimitiveTypeName(output->getType()) << std::endl;
}
// Set some test values to the inputs before executing the script
inputs->getChild("param1")->set<int32_t>(21);
inputs->getChild("param2")->set<float>(2.f);
// Update the logic engine (executes the script we created above)
logicEngine.update();
/**
* After execution, we can get the calculated outputs
* The getters of the values are returned as std::optional
* to ensure the combination of name and type matches an existing input.
*/
std::optional<std::string> result = outputs->getChild("result")->get<std::string>();
if (result)
{
std::cout << *result << std::endl;
}
// To delete the script call the destroy method on the LogicEngine instance
logicEngine.destroy(*script);
return 0;
}