Sign in to follow this  
LogicalError

enums & constants

Recommended Posts

I was wondering, are there any plans to include enums into the language? imho they're cleaner to use than a list of const int's. If the debugging part of angelscript supports it, enums would be easier to debug with since you'd be able to see the enum name instead of some int value.. (just like in visual studio) It would also make it possible to limit the number of possible values you can pass to a function. Also i would like to use an enum i have in my c++ code, in angelscript. Since it's impossible to do this directly, i have to pass each one to angelscript. Unfortunatly, i can't set constants in angelscript.. I could do that using RegisterGlobalProperty, but then i'd need an int for every enum (not a good solution).. Another solution would be to create a script at runtime and compile it. But i'd rather have a function i could call to do something like "const int blaa = 10;" or better: have some mechanism to define enums from the c++ code.. I don't want to hardcode the values in the script because that would require me to edit them in 2 places if i need to change it.. which is asking for trouble.. PS. In case i haven't said it yet: Angelscript is great! :)

Share this post


Link to post
Share on other sites
Currently I recommend that you add a separate script section with the constants when building a script, e.g.:

const char *constants =
"const int blah = 23;"
"const int blah2 = 21;";

void Compile(asIScriptEngine *engine)
{
engine->AddScriptSection("module", "constants", constants, strlen(constants));
engine->AddScriptSection("module", "loaded script", script, strlen(script));
engine->Build();
}

Share this post


Link to post
Share on other sites
All script sections added to the same module share the same scope/namespace, so yes, the "loaded script" will know about the constants.

Script sections added to different modules have different namespaces though, so you would have to add the same constants to each of the modules that needs them.

Regards,
Andreas

Share this post


Link to post
Share on other sites
The last year, i sended to WitchLord a patch to support pseudo enumerations on the engine as part of the constructor arguments patch, with a function named some like RegisterEnumeration or so, it supported enums in the form of

RegisterEnumeration("enum1=0, enum2, enum3, ....")
RegisterEnumeration("enum1, enum2, enum3, ....")
RegisterEnumeration("enum1, enum2=5, enum3, ....")
RegisterEnumeration("enum1=1, enum2=2, enum3=3, ....")


if i have the time, probably next week, i will take a look and see if i still have it somewhere

Lioric

Share this post


Link to post
Share on other sites
Alternativly, a preprocessor can move the responsibility of including the constants-file from the app to the script.

Share this post


Link to post
Share on other sites
If enums are implemented in angelscript, it would be nice though if they're more strongly typed than c++ enums, like in c#
so that
enum A
{
BLAAH
}

enum B
{
BLAAH
}

don't cause a conflict.. the enums could still be used by writing down A.BLAAH and B.BLAAH instead of just BLAAH...

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