How to transfer and update objects using luabind in C ++

I am trying to integrate a scripting engine into my existing project. However, I could not figure out how to pass lua objects with luabind.

For example, I have an entity class, and I want to update them in lua files.

#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; } 

Here is the code. What I want to know is to pass the object objects and the time value in lua and update their positions using their speed and delta time.

+4
source share
1 answer
 luabind::globals(myLuaState)["entities"] = table; 

Basically, luabind::globals(lua) returns a _G lua table that you can manipulate as usual.

+4
source

Source: https://habr.com/ru/post/1314333/


All Articles