Advertisement

int/enum declaration bug?

Started by May 08, 2012 10:21 PM
2 comments, last by WitchLord 12 years, 6 months ago
I was having a problem with a script where enum values were incorrect. Eventually, I found that in the script there was an int with the same name as an enum value, but there was no error indicating so. I guess since enums are pretty much the same as ints so it just overwrote the enum value, right? Is this a bug?
Can you show in what circumstance you got the wrong value? I.e. give an example script that reproduces what you believe to be wrong.

An enum value can be implicitly cast to an integer. If the expected value was an integer or even a float or double, then the enum value and the integer value has equal rights, and the one that is chosen would depend on the scope.

If on the other hand the expected value was an enum, then only the enum should have been allowed, as the compiler should not do implicit casts of integers to enums.

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
This is pretty much what I was doing:

C++:
engine->RegisterEnumValue( "Resource_Sound", "sndJump", 0);

Script:
class objPlayer
{
int sndJump; //Forgot to remove this when I decided to use enums instead;

objPlayer()
{
string text = sndJump;
printf( text); //I expected the enum value of 0 but instead it prints -842108451
}
}

It works as intended if I remove the 'int sndJump'. I just thought that this script should fail to build if this happens.
This is not a bug.

The class member sndJump is in a different scope than the enum value registered by the application. When referring to sndJump in the class constructor it will find the class member first and use that.

To use the enum value in this case you need to define the global scope with "::" or more explicitly the scope of the enum itself with "Resource_Sound::".

The value -842108451 is because the sndJump member wasn't initialized before it was used.


I will however consider this situation. Perhaps I can make some improvements in the compiler to provide adequate warnings when a class member or local variable hides an enum or global variable. I may also be able to provide warning on use of a class member before it is initialized (in a constructor at least).

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