# Luabind operator=

This topic is 2909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I have my struct Vector2D defined like this:

struct Vector2D{    float x, y;    Vector2D &operator=(const Vector2D &rhs)    {        x = rhs.x;        y = rhs.y;        return *this;    }    /* and so on */}

currently I want to bind this struct to luabind. So I called:

luabind::module(mpLua)[    luabind::class_<Vector2D>("Vector2D")    .def(luabind::constructor<>())    .def(luabind::constructor<float,float>())    .def(luabind::self = Vector2D()) // will cause compile error    .def_readwrite("X", &Vector2D::x)    .def_readwrite("Y", &Vector2D::y)];

After reading through some websites, it seems that operator= can't be bound to lua. But the problem is: the lua script somehow does not do '=' correctly:

MyFunc = function(MyVec)    vec2 = Vector2D(0,10)    MyVec = vec2    -- this line does not workend

The constructor works. And it works if i do this instead:

MyFunc = function(MyVec)    vec2 = Vector2D(0,10)    MyVec.X = vec2.X    MyVec.Y = vec2.Yend

I'm not sure why the '=' operator does not work. Can anyone enlighten me?

EDIT: some extra info ^^

I'm calling the function mentioned above in C++ like this:
Vector2D myvec;luabind::object states = luabind::globals(mpLua);states["MyFunc"](&myvec);

UPDATE:
MyFunc = function(MyVec)    vec2 = Vector2D(0,10)    vec3 = vec2 -- somehow this line works    MyVec.X = vec2.X    MyVec.Y = vec2.Yend

So it seems to me that, the equal sign only works for the local variable in lua, while it doesn't work for the variable that i passed to the parameters. Does anyone know where is my mistake?

Thanks

##### Share on other sites
Are you sure vec3 isn't just becoming vec2 and not being assigned the values. Tables in Lua are like pointers. When you pass them around they are like pointers.

So in C what you wrote would be something akin.

Vector* vec2 = new Vector2(0,10);
Vector* vec3 = new Vector2(0,0);
vec3=vec2;

If you examine vec3 you might see that it's values has become that of vec2 but in reality it is vec2. You can verify if this is the case by either doing a equality comparision on them ie.

if (vec2==vec3) then print("same table"); end

or examining the raw ptr itself ie.

print("vec2 = "..tostring(vec2)..", vec3 = "..tostring(vec3));

Good Luck!

-ddn

##### Share on other sites
Quote:
 Original post by ddn3Are you sure vec3 isn't just becoming vec2 and not being assigned the values. Tables in Lua are like pointers. When you pass them around they are like pointers. So in C what you wrote would be something akin.Vector* vec2 = new Vector2(0,10);Vector* vec3 = new Vector2(0,0);vec3=vec2;If you examine vec3 you might see that it's values has become that of vec2 but in reality it is vec2. You can verify if this is the case by either doing a equality comparision on them ie.if (vec2==vec3) then print("same table"); endor examining the raw ptr itself ie.print("vec2 = "..tostring(vec2)..", vec3 = "..tostring(vec3));Good Luck!-ddn

I've tried what you said, so:
MyFunc = function(MyVec)    vec2 = Vector2D(0,10)    vec3 = vec2    if( vec2 == vec3) then print("same") end -- yup it does print the sameend

But if I do this:
MyFunc = function(MyVec)    vec2 = Vector2D(0,10)    MyVec = vec2    if( MyVec == vec2) then print("same") end -- it won't printend

So probably you are right. Since MyVec is a pointer given by C++, so probably the assignment MyVec = vec2 can't be done? It does not give me any error though. It's just as if it does nothing.

##### Share on other sites
I've not tried operator overloading in Lua but i think u can only overload certain operators, maybe the assignment isn't one of them? I'll do more research.

-ddn

##### Share on other sites
i checked the official luabind website:

http://www.rasterbar.com/products/luabind/docs.html#operators

Quote:
 The operators supported are those available in Lua:+ - * / == < <=

btw is there anyway to 'undo' a lua script after calling dofile?

so for example I have many files:

luaL_dofile(L, "AI1.lua");luaL_dofile(L, "AI2.lua");luaL_dofile(L, "AI3.lua");// ... and so on

just assume I have many different type of AI behaviors, but only two or three behaviors may be active at the same time. And each of behaviors is in different lua files. Assume there's no name conflict or whatsoever. But if I were to load all the scripts to the table, I think it will be a waste of memory, because only some will be active, while the rest is just hogging the resources but doing nothing.

Obviously in the actual implementation, I will only do dofile when the behavior is needed. But once it's no longer needed, I want to release the resources. and will only reallocate the resources if it's needed again.

so my question is whether there is a way to 'unload' some of the unused behaviors?

I've searched the forum and google, it seems that I can use luabind::nil, by just setting all the luabind::object from the file I want to unload to luabind::nil. But does it really release the resource from the lua stack?

thanks again :)

##### Share on other sites
There are several ways I've read about. One is to create a new Lua environment per script and scrap that when your done with it. This can be done within a single VM so you don't incur the cost of creating/destroying VMs. I've not use that scheme, but I read about it from the Lua message board archives.

http://lua-users.org/lists/lua-l/

Cleaning up specific lua scripts

The other is to use the package script loader through the keyword require like so

require "Scripts/AIBot"

this will load the script like dofile and put the resulting object in the packages table which i believe u can nil out once your done with. This won't remove any globals which the script has registered so you might still have to create a new environment and restore it afterwards.

Good Luck!

-ddn

##### Share on other sites
thanks a lot for your help! I'll try the code first :)

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999976
×