🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

opConv and opImplConv with bool

Started by
6 comments, last by simong 9 years, 7 months ago

Hi,

I've got a lot of shared_ptr underneath my script objects and because of the new features of 2.29.2 I wanted to try registering the handy "operator bool" that allow testing shared_ptr like this :


if( myShared ){
} 

For some reason I can't make opConv or opImplConv to work for booleans. To make sure I wasn't doing something stupid on the c++ side, I tried doing the same in the script using the code provided in the documentation but without success.

It is an extremely useful feature of shared_ptr in c++ and I'd love to be able to expose the same feature for the classes I register to angelscript.

Here's what I tried in c++ :


engine->RegisterObjectMethod( "ObjectRef", "bool opImplConv() const", asMETHODPR(ObjectRef, operator bool, () const, bool ), asCALL_THISCALL ); 

and in Angelscript:


class MyObj
{
    double myValue;
    double opImplConv() const  { return myValue; }
    bool opImplConv() const  { return myValue > 0; }
    // bool opConv() const  { return myValue > 0; }
};

MyObj c;
c.myValue = 0.12345;

double cDouble 	= c; // this works
bool 	cBool	= c; // this doesn't and gives "MainSection (66, 16) : ERR  : Can't implicitly convert from 'MyObj&' to 'bool'."

Is this a limitation of the new conversion operator or am I missing something obvious?

Thanks in advance!

Simon.

Advertisement

You're not doing anything wrong.

The AngelScript language simply doesn't allow type conversions to boolean, because it is a very common bug in languages that do allow it (e.g. C++) to accidentally use a value as a boolean where it shouldn't be.

I may relax this in an upcoming release (the updates in C++11/14 standard has shown me one way of doing it safely), but currently it is not supported.

I see I've forgetten to document this restriction in the article above. It is documented in the use of the asBEHAVE_VALUE_CAST behaviour though. I'll have this updated in the manual for the next release.

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

Thanks,

This makes sense but wouldn't it be useful to have an alternative when you know what you are doing? I mean if you deliberately write a bool operator in your class, I guess it is probably because you want your class to behave differently and it means that the conversion to bool is well defined as well.

I don't know maybe in this case adding an opBool to the language would make more sense?

Following the above discussion, I am too in a situation where a bool conversion would be very usefull.

I have a lot of system objects registered as variables into Angel Script, that represent physical digital inputs from hardware.

What I am currently trying to do is to allow operation like:

out1 = In3 || (In4 && in7);

or

if(In3 && In8) ...

Is this even possible without an implicit bool conversion?

Could be possible to allow this type of operations maybe subject to a configuration variable?

Thanks.

Mau.

Yes, I'll probably add an engine property to permit configuring the compiler allow implicit conversions to bool.

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

That would be great!

Thanks.

Mau.

I've changed the compiler to allow registered value types to be implicitly converted to bool using 'bool opImplConv()'. This is always turned on, i.e. no engine property has to be set.

Reference types can be explicitly converted to bool using 'bool opConv()'. I not yet decided if reference types should allow implicit conversions to bool, since if the handle is null an exception would the thrown. (perhaps I could allow it, but warn if the compiler determines that the expression might be null)

You can get the new version from the svn revision 2079.

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

That's great! Thanks a lot Andreas.

This topic is closed to new replies.

Advertisement