[英]How to pass and update objects with luabind in c++
我正在尝试将脚本机制集成到我现有的项目中。但是我无法理解如何使用luabind将对象传递给lua。
例如,我有一个实体类,我想在lua文件中更新它们。
#include <stdio.h>
#include <ctime>
#include <iostream>
#include <string>
#include <list>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <luabind/luabind.hpp>
class Entity
{
public:
Entity(){}
~Entity(){}
void setSpeed(double adSpeed){m_dSpeed = adSpeed;}
void setPosition(double adPosition){m_dPosition = adPosition;}
double getSpeed(){return m_dSpeed;}
double getPosition(){return m_dPosition;}
private:
double m_dSpeed;
double m_dPosition;
};
int main() {
// Create a new lua state
lua_State *myLuaState = lua_open();
// Connect LuaBind to this lua state
luabind::open(myLuaState);
// Export our class with LuaBind
luabind::module(myLuaState) [
luabind::class_<Entity>("Entity")
.def(luabind::constructor<void>())
.property("m_dSpeed", &Entity::getSpeed, &Entity::setSpeed)
.property("m_dPosition", &Entity::getPosition, &Entity::setPosition)
];
luabind::object table = luabind::newtable(myLuaState);
Entity* entity1 = new Entity;
table["Entity1"] = entity1;
//How to pass entity object to lua
luabind::luaL_dofile(myLuaState, "UpdatePosition.lua");
lua_close(myLuaState);
return 1;
}
这是代码。我想学习的是将实体对象和时间值传递给lua,并使用它们的速度和增量时间来更新它们的位置。
luabind::globals(myLuaState)["entities"] = table;
基本上, luabind::globals(lua)
返回_G
lua表,你可以照常操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.