Advertisement

Question about asOBJ_VALUE asOBJ_CLASS and constructors

Started by September 22, 2010 08:58 AM
3 comments, last by pifor106 14 years, 2 months ago
Hi once again !

I've stumble upon something awkward when dealing with constructors and I would like to know more about the issue.

Here's my registration code :

Global(m_registry)
[
Object< ConstructorTest >("ConstructorTest")
[
// Register our only constructor
Constructor< ConstructorTest, const u32 >("void f(const int)"),

// Register some methods ...
Method("int GetToken()", &ConstructorTest::GetToken)
]
];

m_registry.Finalize();

This ultimately calls :

m_engine->RegisterObjectType("ConstructorTest", sizeof(ConstructorTest), asOBJ_VALUE | asOBJ_APP_CLASS_D);
m_engine->RegisterObjectBehaviour("ConstructorTest", asBEHAVE_DESTRUCT, "void f()", dtorInvoker, asCALL_GENERIC);
m_engine->RegisterObjectBehaviour("ConstructorTest", asBEHAVE_CONSTRUCT, "void f(const int)", ctorInvoker, asCALL_GENERIC);
m_engine->RegisterMethod("ConstructorTest", "int GetToken()", methodInvoker, asCALL_GENERIC);

When I load my test code ( since I'm developping the registration framework above using unit tests ), I end up with the following error :

error: Failure in Test_registering_a_constructor_with_one_arg: 2 error logged from AngelScript:
Type 'ConstructorTest' is missing behaviours
Invalid configuration

If I do add a default constructor to the above registration, everything works fine.

Now, I must say that I'm a bit puzzled. I stepped in the code and the registration checks if we have a default ctor/dtor, if not it reports an error. My question is, how can I expose a type which is a class but has no default ctor to the engine. I'm quite a heavy user of those types of object since constructors/destructors are one of the most important features of the C++ ( RIAA ... ) and I would like to do the same in AngelScript if possible. Must I use the asOBJ_SCOPED flag ? If so, let's say I want to have this behavior by default, should I always use the asOBJ_SCOPED flag ?

Thanks for any input !

Pierre

I'm pretty sure you need the asOBJ_APP_CLASS_CONSTRUCTOR flag as part of your RegisterObjectType() call.
Advertisement
Thanks, I tried that earlier and It didn't do any good.

The fact is that the engine wants a constructor when using a class, but it wants the default ctor and none other, which in my case I do not want to add.

Pierre
I haven't prepared AngelScript to work with value types that doesn't have default constructors. Removing that restriction is possible, but it will require several changes that I don't have fully envisioned at this moment.

Sounds like you want to have an id passed to the constructor. Is there not a default id you can use? Just because your C++ class doesn't have a default constructor doesn't mean you can't register one to AngelScript. The constructors must be wrapped in a global function anyway, so adding a default value would be a trivial task.



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

Ok then, thanks for the answer. Actually, that is not such a major issue to me, as long as it's known and expected. Since I'm actually implementing the framework on top of our engine, and not using it per-se, It's not so much an issue since I will always generate a default constructor for all value classes within my registration layer which wraps the engine. Actually, I think I prefer this, to letting the end user determine if the default constructor should be declared or not. I'm already registering the destructor automatically anyways, so that symmetrizes up things :)

As always, thanks for the quick answer !

This topic is closed to new replies.

Advertisement