🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to add global constants from C++?

Started by
7 comments, last by Deyja 18 years, 8 months ago
I give up. I searched and searched and didn't come up with anything. Here 's my problem:

enum EventType
{
    etOnDoThis,
    etOnDoThat
};
How do I bind this in script? Ideally, I would create two global const int's named etOnDoThis and etOnDoThat but I can't find a way to do this. Is there any way? As a workaround I 've created two static const variables and bound those using RegisterGlobalProperty() but this is not good... Regards, Yiannis.
Advertisement
enums are not yet supported in AngelScript. I know they are important however, and I plan on implementing them soon. That and true constants, registered from the application without the need for global variables.

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

Quote: Original post by WitchLord
enums are not yet supported in AngelScript. I know they are important however, and I plan on implementing them soon. That and true constants, registered from the application without the need for global variables.


I don't really care about enums as such, but more for constant globals. Could look like:

engine->RegisterGlobalConstant("int aValueThatNeverChanges", 138);


Is there any other (or smarter) workaround for what I did already?
Thanks anyway :)
Well the obvious.

Just have the constant and somewhere set the value of the constant to a global.

then implement the global as "const .." for angelscript

Edit: Of course that would just kill the reason to have a constant anyways.

------ XYE - A new edition of the classic Kye
Quote: Original post by Vexorian
Well the obvious.

Just have the constant and somewhere set the value of the constant to a global.

then implement the global as "const .." for angelscript


Thanks for the answer, but as stated above, I 'd like to create some global script vars to match some C++ enums. This means I don't have variables for them in C++ side (they 're just enums).
My workaround was to actually create one static var per enum and register this to the script as global const. But it's not elegant, hence the question...
I see, I didn't read your question correctly and assumed you wanted to use things declared with #define inside scripts.

I think that what you did is the best aproach
------ XYE - A new edition of the classic Kye
Actually, there is a better way. And that is to write a sort of script header with the constant declarations. Then you compile this script header together with your scripts. That way the script compiler will be able to treat the constants as true constants, and not just read-only variables, i.e. it will be able to optimize expressions and use them in switch cases.

Example:

const char * constants =
"const int myConst = 1;"
"const float myOtherConst = 1.4f;";

// Add both the normal script and the constant declarations to the same module
engine->AddScriptSection(0, "constants", constants, strlen(constants), false);
engine->AddScriptSection(0, "script file", script, strlen(script), false);

// The script will be able to see and use the constants
engine->Build(0);

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

Quote: Original post by WitchLord
Actually, there is a better way. And that is to write a sort of script header with the constant declarations. Then you compile this script header together with your scripts. That way the script compiler will be able to treat the constants as true constants, and not just read-only variables, i.e. it will be able to optimize expressions and use them in switch cases.

Example:

const char * constants =
"const int myConst = 1;"
"const float myOtherConst = 1.4f;";

// Add both the normal script and the constant declarations to the same module
engine->AddScriptSection(0, "constants", constants, strlen(constants), false);
engine->AddScriptSection(0, "script file", script, strlen(script), false);

// The script will be able to see and use the constants
engine->Build(0);


I originally tried with ExecuteString() which obviously didn't work.
Thanks a lot for the tip :)
I'd prefer a preprocessor solution over Witchlord's, but that's probably because I wrote the preprocessor. It's on the addon's page at www.angelcode.com It has a simple interface for 'compiler-set' defines; things like '__DEBUG__' in MSVC. Of course, #defined constants have subtly different properties than real constants, but for enums they should work identically.

This topic is closed to new replies.

Advertisement