Advertisement

(Proposition): Problem with No Default constructor.

Started by September 05, 2009 08:22 AM
4 comments, last by Wracky 15 years, 5 months ago
OK sorry about this edit... I think I figured out my problem. I want to register an object by reference, and I want to call the object's constructor with parameters. I figured out however, that this is not possible without having a default constructor aswell! This is a problem for me, since not all objects have default constructors. When I register the object like this:

	r = scriptEngine.RegisterObjectType("Object", 0, asOBJ_REF); assert( r >= 0 );
	r = scriptEngine.RegisterObjectBehaviour("Object", asBEHAVE_FACTORY, "Object @f(int)",  asFUNCTIONPR(asObjectFactoryInt, (int) , Object*),asCALL_CDECL); assert( r >= 0 );
It doesn't work... it gives me the error:

ERROR: In D:/testscript.as: Data type can't be 'Object' at row: 3 col: 9
However after I add this line:

r = scriptEngine.RegisterObjectBehaviour("Object", asBEHAVE_FACTORY, "Object @f()",  asFUNCTION(asObjectFactory),asCALL_CDECL); assert( r >= 0 );
It works. This default constructor however, never gets called... but seems to be required to make the one with parameters, work. Is there anything I can do about this? I can't/don't want to facilitate default constructors for all my objects. Thanks! Kind regards, Wracky. [Edited by - Wracky on September 6, 2009 7:33:09 AM]
http://www.piko3d.net
Ok so I've looked into this, and indeed found that angelscript does not allow you to use a referenced class without a default constructor. I missed this in the docs. Could only find a reference to this, in the docs for objects by value, where it states that object by value DO NOT need a default constructor. :-/

I've looked into the angelscript code for a bit, and this limitation feels artificial... The behaviours use a .factory member and a .factories member, which seem to be interchangeable in a lot of places.

I've tried changing the CanBeInstanciated() function to also return true when the object has other factories beside the default one. I know this will probably ruin other cases, but it worked in mine. I've looked a bit further to where the beh->factory member is used, and it seems to be mostly used to call the default constructor for script classes.

I have the feeling it should be possible to take this limitation out, but this would probably also mean that you'd have to have some way to know when you can or cannot instantiate the class without pushing parameters.... And in cases where there is no default constructor, deciding whether or not this object can be instantiated would depend whether or not the correct param-list is available.

Angelscript was probably designed like this for a reason, and perhaps there are pitfalls that I fail to see here. Looking at the code, I realise that this is probably not easily done, and not a trivial task, but not being able to work without default constructors is somewhat of a problem for me :-/

I'll just have to see if I can make it work like this....
I really like the AS syntax and the possibilities for scripting that AS has and I kinda don't want anything else but this is a big hurdle for me to take.

Thanks for reading!
Kind regards,

Wracky
http://www.piko3d.net
Advertisement
Still digging through the AS source code :D
I'm just brainstorming an dreaming of a world where AS would function without mandatory default constructors ;-) I would really like to know if this is worth pursuing, or if I'm just not getting the point, and this is not possible. I'm not the expert here!

What I find interesting, is that in the function CallDefaultConstructor(), there is a case that checks for one. If there is no default constructor, this function just exits.

Now from my point of view, it looks like I could use this, to perhaps return a value saying if the constructor was actually called or not. In the function CompileDeclaration(), I could then check on this, clean up the stuff it allocated up till then with DeallocateVariable(), and give an error like it does now when the object cannot be instanciated.

I understand that this is not the only thing going on with default constructors, and that there is perhaps a reason why "no default" cannot be done, but I've been digging around to see if I could make it work, and this is what I found so far:

In a lot of places, there is code to check if the constructor should be called with or without parameters. If this is already checked on, wouldn't it also be possible to error out if the scripter specified a default constructor when there is none? One of the "TODO" found there, is that there's some duplicate code there too which could be resolved.

The function asCDataType::CanBeCopied() checks for the default constructor. My question is: Couldn't it also use a copy constructor here to copy? Like the std::vector does for instance.

Aside from that, I've found that the functions for script objects use the default constructor a lot directly. I have not looked into this enough to even dare to estimate if this can be worked around too.... but if the other cases COULD be resolved without a default constructor, that would mean, without looking at script classes, that the "must have default constructor" limitation then only applies to the scripted classes... which would be great in my book :D

IF it would be possible to let AS function for C++ classes without default constructors, I would be very happy! I would also offer to help out in implementing it if it is a lot of work!

Please let me know if I'm just talking crazy, or if this could be done, and if so, what the problems are... I find this really interesting :)

Sincerely,
Wracky.
http://www.piko3d.net
It would definitely be possible to change the library to support registered classes without a default constructor. The only reason it is not already supported is that it was easier to just assume all classes would have default constructors.

I think you've found pretty much all of the current problems with not having the default constructor.

Regarding the copy constructor. This is an optimization that I had planned for a later time, but it would probably be necessary to add the support for no default constructor.

In other cases the code should probably just give an error if the script tries to instanciate the class without arguments if there is no default constructor.

If you want to make the necessary changes I'd be more than happy to add them to the library.

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

Cool! :D
I'll get to work then. I don't have a clear view yet of what needs to be done for copy constructors to work.. but I'll look into that. If you could give me some pointers on what needs to be done, that would be great :)

I don't know exactly how much work it is, or when I'm gonna be done since I'll be doing this in my spare time, but I'll try and make it quick ;-)

Thanks!
http://www.piko3d.net
Hey Everyone,

Just a small heads-up on what I'm doing.
Witchlord here helped me a bit with what's what in the Angelscript code,
and I think I have "No default constructor" working now on my PC for registered objects. I did a lot of reading in the source code to learn about the structure, and the actual changes turned out to be not that much work :)

Now I'm going to look at the script classes to see if I can make those work without default constructors too.

I'm currently fighting between working on AS and a Heroes of Might and Magic V addiction, so I'm sorry if things go a bit slow sometimes :-) but I think it shouldn't be too much work..

Best regards!
Wracky
http://www.piko3d.net

This topic is closed to new replies.

Advertisement