The Lua Bind Function example demonstrates the following:
- How to add a new Lua function that can be used with scripted tasks.
- How to use luabind to bind classes and class methods to Lua scripts.
- How to add objects that can be referenced in Lua scripts.
- How to write a plugin that can be used with a back-end.
The Add Lua Function example adds a new Lua bound function called luaExamplePrintMessage and a new object example that can reference this call in a Lua script as example:luaExamplePrintMessage.
Since this example is loaded as a plugin, it can be used in conjunction with the released VR-Forces application. Make sure to enable the addLuaFunction example from the launcher application.
Usage
Select Settings, Plugins... and on the Plugins Editor page, use the combo box at the top of the page to select the plug-in. Then enable the Load.
To view the new behavior:
-
Load Scenario...
-
Create a new scripted task. Scripted tasks are accessed from the Simulation menu item.
-
Edit the script for the scripted task
-
In the init method place a call example:luaExamplePrintMessage("Hello World!").
-
Task an entity to perform the new scripted task.
-
The entity information window and the back-end console window should display Hello World!
Lua Scripting Documents
Documentation of Lua functions for scripted tasks has its own help system.
Plugin Entry Points
#include "luabind/out_value_policy.hpp"
{
public:
AddLuaFunctionInterfaceExtension()
{
}
virtual ~AddLuaFunctionInterfaceExtension()
{
}
{
return new AddLuaFunctionInterfaceExtension;
}
virtual void printMessage(const std::string& message)
{
DtWarn << myEntity->objectName().c_str() << ": " << message.c_str() << std::endl;
myEntity->objectConsoleInfo() << message << std::endl;
}
virtual std::string multipleReturn(bool b, int& someNumber, bool& success)
{
if (b)
{
someNumber = 40;
success = true;
}
else
{
someNumber = 10;
success = false;
}
return "return string";
}
lua_State* L)
{
myEntity = entity;
luabind::module(L)
[
luabind::class_<AddLuaFunctionInterfaceExtension>("AddLuaFunctionInterfaceExtension")
.def("luaExamplePrintMessage",
&AddLuaFunctionInterfaceExtension::printMessage)
.def("luaExampleMultiReturn", &AddLuaFunctionInterfaceExtension::multipleReturn,
luabind::pure_out_value(_3) + luabind::pure_out_value(_4))
];
luabind::globals(L)["example"] = this;
}
protected:
};
extern "C" {
{
}
{
return true;
}
{
}
}