简体   繁体   中英

How to pass and update objects with luabind in c++

i am trying to integrate a scripting mechanism to my existing project.However i could not understand how to pass objects to lua 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 learn is to pass entity objects and a time value to lua and update their positions by using their speeds and delta time.

luabind::globals(myLuaState)["entities"] = table;

基本上, luabind::globals(lua)返回_G lua表,你可以照常操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM