enums & constants

Started by
8 comments, last by LogicalError 19 years, 1 month ago
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! :)
Advertisement
Yes, I plan to add enums and constants to the library. I can't say when though.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Ok, cool..
Right now i'm adding x number of const int's to every script i compile :(
Too bad globals aren't simply known in every script in the same build..
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();
}

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

hmm yes, but then it doesn't know the constants in "loaded script"
unless there's something i don't know?
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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

hmm i tought i tried that and it didn't work...
i'll try again then

edit: works :)

[Edited by - LogicalError on February 10, 2005 1:50:44 PM]
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
Alternativly, a preprocessor can move the responsibility of including the constants-file from the app to the script.
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...

This topic is closed to new replies.

Advertisement