• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ricky C.

Enum collision across namespaces

6 posts in this topic

I've never been a fan of C++'s preference to slam enum values into the global namespace, thus I use namespaces in my C++ code to limit that progression. So it was with delight that I upgraded my AS as soon as I found out that namespaces had come. However I just found a problem. Here's some C++-based pseudocode giving an example of what I do:
[CODE]
// 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) {
...
}
[/CODE]

Now, to do the same thing in AS when I'm registering my interface:
[CODE]
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);
[/CODE]

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:
[CODE]
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);
[/CODE]

--

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++. Edited by Andreas Jonsson
2

Share this post


Link to post
Share on other sites
The offending block of code from as_builder.cpp line 3943

[CODE]
asCObjectType *asCBuilder::GetObjectType(const char *type, const asCString &ns)
{
// TODO: namespace: Registered types should also allow namespaces
asCObjectType *ot = engine->GetObjectType(type);
if( !ot && module )
ot = module->GetObjectType(type, ns);
return ot;
}
[/CODE]

It checks for the type by name only in engine. This check causes it to return the type registered under the first namespace rather than checking with the namespace as well. Edited by adam4813
2

Share this post


Link to post
Share on other sites
Additionally found that object types suffer this issue. Both lines 1426 snf 1433 in as_scriptengine.cpp are lacking checks for namespace
[CODE]
if( objectTypes[n] && objectTypes[n]->name == typeName ) // 1426
if( templateTypes[n] && templateTypes[n]->name == typeName ) // 1433
[/CODE]

After patching them to read:
[CODE]
if( objectTypes[n] && objectTypes[n]->name == typeName && objectTypes[n]->nameSpace == defaultNamespace ) // 1426
if( templateTypes[n] && templateTypes[n]->name == typeName && templateTypes[n]->nameSpace == defaultNamespace ) // 1433
[/CODE]
The parse continues on and errors out at my previous reply. Edited by adam4813
2

Share this post


Link to post
Share on other sites
Thanks to both of you. I'll investigate the problem reported and have it fixed as soon as possible.

In the meantime you may want to set the engine property asEP_REQUIRE_ENUM_SCOPE to true. This changes the compiler to only accept the enum values if they actual enum type is informed with the scope operator.

[code]
enum MYENUM
{
VALUE
}

void func()
{
MYENUM v;

v = VALUE; // With asEP_REQUIRE_ENUM_SCOPE this will give an error
v = MYENUM::VALUE; // OK
}
[/code] Edited by Andreas Jonsson
1

Share this post


Link to post
Share on other sites
I've checked in the fixes for these problems in revision 1315.

There were a few more places that needed adjustments, but now it should be working properly. Let me know if you find any other problems.

Thanks,
Andreas
0

Share this post


Link to post
Share on other sites
Thanks Andreas!

I've not tested, but it might be good to verify that typedefs get namespaced as well. Currently they are of limited use, but hopefully...
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0