Jump to content

  • Log In with Google      Sign In   
  • Create Account


angelscript value type registration


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 viepr08v10   Members   -  Reputation: 121

Like
0Likes
Like

Posted 31 December 2012 - 04:23 PM

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, 31 December 2012 - 04:26 PM.


Sponsor:

#2 viepr08v10   Members   -  Reputation: 121

Like
0Likes
Like

Posted 31 December 2012 - 05:08 PM

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


#3 slicer4ever   Crossbones+   -  Reputation: 3468

Like
0Likes
Like

Posted 01 January 2013 - 01:44 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS