angelscript value type registration

Started by
1 comment, last by slicer4ever 11 years, 3 months ago

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

Advertisement

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);

just as an FYI, their is an dedicated board for angelscript here on gamedev: http://www.gamedev.net/forum/49-angelcode/

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

This topic is closed to new replies.

Advertisement