Advertisement

Problems creating instance of registered class

Started by September 08, 2009 04:47 PM
2 comments, last by WitchLord 15 years, 1 month ago
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!
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.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Thanks very mutch!

Cheers
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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement