Sign in to follow this  
viepr08v10

angelscript value type registration

Recommended Posts

I'm having a problem registering my vector class by value, in the current svn version of angelscript.

 

here is the relevant c++ 

 

class Vec3f {
public:
    float x,y,z;
    Vec3f(float x=0,float y=0,float z=0) {
        this->x = x;
        this->y = y;
        this->z = z;
    }

    Vec3f(const Vec3f &other) {
        this->x = other.x;
        this->y = other.y;
        this->z = other.z;
    }

    Vec3f operator+(const Vec3f &other) {
        return Vec3f(x+other.x,y+other.y,z+other.z);
    }

    Vec3f &operator =(const Vec3f &other) {
        this->x = other.x;
        this->y = other.y;
        this->z = other.z;
        return *this;
    }
};

static void Vec3fDefaultConstructor(Vec3f *self) {
    new(self) Vec3f();
}

static void Vec3fCopyConstructor(const Vec3f &other,Vec3f *self) {
    new(self) Vec3f(other);
}

static void Vec3fInitConstructor(float x, float y,float z,Vec3f *self) {
    new(self) Vec3f(x,y,z);
}

static void Vec3fDestructor(Vec3f *self) {
    self->~Vec3f();
}

int registerMath(asIScriptEngine *engine) {
    int r = engine->RegisterObjectType("Vec3f",sizeof(Vec3f),asOBJ_VALUE | asOBJ_APP_CLASS_CAK);
    assert(r>=0);

    //Vec3f poperties
    r = engine->RegisterObjectProperty("Vec3f","int x",asOFFSET(Vec3f,x));
    assert(r>=0);
    r = engine->RegisterObjectProperty("Vec3f","int y",asOFFSET(Vec3f,y));
    assert(r>=0);
    r = engine->RegisterObjectProperty("Vec3f","int z",asOFFSET(Vec3f,z));
    assert(r>=0);

    // Register the constructors
    r = engine->RegisterObjectBehaviour("Vec3f",asBEHAVE_CONSTRUCT,"void f()",asFUNCTION(Vec3fDefaultConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    r = engine->RegisterObjectBehaviour("Vec3f",asBEHAVE_CONSTRUCT,"void f(const Vec3f &in)",asFUNCTION(Vec3fCopyConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    r = engine->RegisterObjectBehaviour("Vec3f",asBEHAVE_CONSTRUCT,"void f(float,float,float)",asFUNCTION(Vec3fInitConstructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    r = engine->RegisterObjectBehaviour("Vec3f", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Vec3fDestructor), asCALL_CDECL_OBJLAST);
    assert(r>=0);

    //Vec3f methods
    r = engine->RegisterObjectMethod("Vec3f","Vec3f &opAssign(const Vec3f &in)",asMETHODPR(Vec3f,operator =,(const Vec3f &),Vec3f &),asCALL_THISCALL);
    assert(r>=0);
    r = engine->RegisterObjectMethod("Vec3f","Vec3f opAdd(const Vec3f &in)",asMETHOD(Vec3f,operator +),asCALL_THISCALL);
    assert(r>=0);

    return r;
}

And the test script

void main() {
	Vec3f v1(5,5,5);
	Vec3f v2(5,5,5);
	
	print("Vec3f("+v1.x+","+v1.y+","+v1.z+")\n");
}

 

And the output

Vec3f(1084227584,1084227584,1084227584)

 

I think it might have to do with either the placement new or the  asOFFSET but I'm not sure.

 

P.S. I'm using Mingw 4.7.0

Edited by viepr08v10

Share this post


Link to post
Share on other sites

Never mind i was tired last night and i tried registering float properties as int that was stupid. 

r = engine->RegisterObjectProperty("Vec3f","int x",asOFFSET(Vec3f,x));
assert(r>=0);
r = engine->RegisterObjectProperty("Vec3f","int y",asOFFSET(Vec3f,y));
assert(r>=0);
r = engine->RegisterObjectProperty("Vec3f","int z",asOFFSET(Vec3f,z));
assert(r>=0);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this