Sign in to follow this  
Friggle

Problems creating instance of registered class

Recommended Posts

Hi, I'm currently experiencing problems with the following example code.
SerialPort port(1, 100);

void main()
{
    if (port.Open())
    {
        MessageBox("COM port opened successfully");
    }
    else
    {
        MessageBox("Unable to open COM port!");
    }
}
I get the AS compiler message: ERR : Data type can't be 'SerialPort' 'SerialPort' is a class registered through the following code.
r = engine->RegisterObjectType("SerialPort", 0, asOBJ_REF); ASSERT( r >= 0 );
    r = engine->RegisterObjectBehaviour("SerialPort", asBEHAVE_FACTORY, "SerialPort @f(uint16 comPort, uint rxFifoSize)", asFUNCTION(MbSerial_Factory), asCALL_CDECL); ASSERT( r >= 0 );
    r = engine->RegisterObjectBehaviour("SerialPort", asBEHAVE_ADDREF, "void f()", asMETHOD(CMbSerial,AddRef), asCALL_THISCALL); ASSERT( r >= 0 );
    r = engine->RegisterObjectBehaviour("SerialPort", asBEHAVE_RELEASE, "void f()", asMETHOD(CMbSerial,Release), asCALL_THISCALL); ASSERT( r >= 0 );
here's the factory function I use:
CMbSerial *MbSerial_Factory(WORD comPort, DWORD rxFifoSize)
{
    return new CMbSerial(comPort, rxFifoSize);
}
Any hints what could go wrong here? I'm using AngelScript 2.17.0 and VC++ 6.0 IDE Thanks for reading!

Share this post


Link to post
Share on other sites
This is related to this discussion.


Currently AngelScript won't let the scripts instanciate objects that don't have a default constructor or factory, i.e. with zero arguments. This may be improved in a future version (see above thread), but I'll update the error message right a way so that it is more clear.


To work around this, you can either add a default factory to your SerialPort object. Or you can provide a global function (same as your factory function) that instanciates the port and then returns a handle to the object, and the script would then call that function instead of instanciating the SerialPort locally.


Share this post


Link to post
Share on other sites
I've checked in a contribution by Wracky that should allow you to register reference types without a default factory behaviour.

The compiler will now give a proper error message if it needs to use the default factory and it is not available, but it is no longer required for instanciating the objects.

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