Advertisement

Automatic integer - bool convertion

Started by March 15, 2009 06:07 AM
5 comments, last by WitchLord 15 years, 8 months ago
Hello! One of the things I find utterly strange in AngelScript is the inability to simply write:
int a;
...
if (a)
{

Why am I always forced to write:
int a;
...
if (a != 0)
{

or
...
if ((a & 1) != 0)
{

instead? This is very unfriendly to end-programer and counter-intuitive. Is there any possibility this feature will be implemented in the near future?
You're thinking like a C++ programmer. It's not so counter intuitive for non-C++ programmers, and actually quite common cause for bugs with inexperienced programmers.

However, I can add a engine property to allow the application developer to choose if these conversions should be allowed or not.

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
Like Andreas said, it is a cplusplusy thing. As soon as you try out C# you'd find the same thing as AngelScript.
ints are not bools and you are making a technical assumption that the value of False is the same as integer zero and True is any other value. (which, for the most part is always right)
it's not only it's a C++ thing, it's also a C thing, a PHP thing, a &#106avascript thing, insert_language_of_your_preference thing.<br><br><!--QUOTE--><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!--/QUOTE--><!--STARTQUOTE--><i>Original post by midnite</i>As soon as you try out C#<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE-->I have and I don't think I'll ever come back to it :)<br>
On this subject, one thing that always bugs me off is that some operations with enum defined values generate errors at compile time. We use enums to "feed" the script with all kind of predefined values, many being bitflags, and operating with these bitflags can be pretty ugly at times. Example of compiling:

gametype.spawnableItemsMask &= ~G_INSTAGIB_NEGATE_ITEMMASK;

The negation is a illegal operation. I asume this is cause it tries to negate the enum value itself, so it fails. I use this trick to force it to generate a copy of itself to operate (well, that's my guess):

gametype.spawnableItemsMask &= ~uint(G_INSTAGIB_NEGATE_ITEMMASK);

Then it works. But it's pretty ugly to have to add the cast every time we want to use some of the predefined bitflag constants. Do you think it would be possible/convenient to force it to generate the copy on that kind of operations?

[Edited by - jal_ on March 16, 2009 6:08:27 AM]
Yes, I'll add this. bitwise complement should work for enum values as well.

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
I've checked in the fix for bitwise complement and also unary negate for enum values.

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