// Create a namespaced enumeration
namespace MY_ENUM {
enum TYPE {
VALUE_A,
VALUE_B,
}
}
// And another
namespace ANOTHER_ENUM {
enum TYPE {
VALUE_A,
VALUE_B,
}
}
// Example usage of the types
ANOTHER_ENUM::TYPE MyFunction(MY_ENUM::TYPE);
// Example usage of the values
if (MyFunction(MY_ENUM::VALUE_B) == ANOTHER_ENUM::VALUE_A) {
...
}
Now, to do the same thing in AS when I'm registering my interface:
asIScriptEngine* as_engine = ....; // Go get the current pointer.
int ret = 0;
ret = as_engine->SetDefaultNamespace("MY_ENUM"); assert(ret >= 0);
ret = as_engine->RegisterEnum("TYPE"); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_A", 0); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_B", 0); assert(ret >= 0);
ret = as_engine->SetDefaultNamespace("ANOTHER_ENUM"); assert(ret >= 0);
ret = as_engine->RegisterEnum("TYPE"); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_A", 0); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_B", 0); assert(ret >= 0);
// Clean up the namespace
ret = as_engine->SetDefaultNamespace(""); assert(ret >= 0);
What was my surprise when the second RegisterEnum("TYPE") caused the assert to fail and the following message to be placed in my AS log:
Failed in call to function 'RegisterEnum' with 'TYPE'
Since both example enums are supposed to be in separate namespaces, they shouldn't have any collisions... Global properties and functions have no such issue:
float e = 2.71828183f;
float p = 3.14159265f;
ret = as_engine->SetDefaultNamespace("WONDER"); assert(ret >= 0);
ret = as_engine->RegisterGlobalProperty("float Flubber", &e); assert(ret >= 0);
ret = as_engine->RegisterGlobalFunction("void function()", asFUNCTION(funcA), asCALL_CDECL); assert(ret >= 0);
ret = as_engine->SetDefaultNamespace("AMUSEMENT"); assert(ret >= 0);
ret = as_engine->RegisterGlobalProperty("float Flubber", &p); assert(ret >= 0);
ret = as_engine->RegisterGlobalFunction("void function()", asFUNCTION(funcB), asCALL_CDECL); assert(ret >= 0);
--
After hacking my registration to get it operable I found myself playing hide-and-seek with my enum values. For some reason I found my enum value hiding at MY_ENUM::TYPE::VALUE_A instead of where I expected, based on how C++ operates, at MY_ENUM::VALUE_A. This is a good thing, and quite a nice change, but it's almost entirely unlike C++.