Connecting the C++ class with the LUA table -
i trying connect 3d engine lua (5.1) parser. example, have lua class of vec3 , have c++ class of vec3. want them work eachother.
this (part) of c++ class:
class vec3 { public: vec3() {} vec3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {} vec3 operator+(const vec3 &b) { return vec3(x + b.x, y + b.y, z + b.z); } float dot(const vec3 &b) { return x * b.x + y * b.y + z * b.z; } float x, y, z; }
this (limited) lua version:
vec3 = {}; vec3.__index = vec3; local mt = {} mt.__call = function(class_tbl, ...) local obj = {} setmetatable(obj, vec3); vec3.init(obj, ...); return obj; end vec3.init = function(obj, x, y, z) obj.x, obj.y, obj.z = x, y, z; end setmetatable(vec3, mt); function vec3:__tostring() return "(" .. self.x .. ", " .. self.y .. ", " .. self.z .. ")"; end function vec3:__add(b) return vec3(self.x + b.x, self.y + b.y, self.z + b.z); end function vec3:dot(b) return self.x * b.x + self.y * b.y + self.z * b.z; end
i think question quite obvious: want able use vec3's in c++ code, example position nodes or other stuff , want able make these available in lua lua-programmer can math vec3's , send them c++. want able construct vec3 in lua , send c++ understood vec3 class.
to achieve this, think need construct above lua table in c instead of in lua , need create function "push" , "pop" send them lua , retrieve them lua.
but trials fail.
can me work?
dirk.
why not try use c++ packages luabind or luabridge? in you can access lua data c++ , vice versa.
Comments
Post a Comment