tags:

views:

63

answers:

1

Hi,

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.

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

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

sbk
When i add this line into code.Code crashes at runtime because of unhandled exception.
Qubeuc
@Qubeuc: What's the exception?
sbk