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
In the Launcher, click the Plug-ins button to bring up the Plug-ins Selection dialog. Enable the plugin. Then launch VR-Forces.
To view the new behavior:
-
Create a new 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 to example:luaExamplePrintMessage("Hello World!").
-
Create an entity, bring up the entity information window and set the notify level to Info.
-
Task the 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->markingText().string() << ": " << 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";
}
bool alreadyInitialized(lua_State* L, const char* moduleName) const
{
luabind::object obj = luabind::globals(L)["example"];
if (luabind::type(obj) != LUA_TNIL)
{
return true;
}
return false;
}
lua_State* L)
{
if (alreadyInitialized(L, "example"))
{
return;
}
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;
}
{
}
}