Advertisement

C++ header file shared with scripts

Started by December 15, 2011 11:09 AM
5 comments, last by WitchLord 12 years, 9 months ago
I have one header file with enums which we use in our game engine. I would like to use this enums in the scripts as well. I would like to avoid situation when modification in enums requires modification in some other place where I register our interface to the AngelScript engine, because it's unnecesary duplication of code and work. Is there any way to make this process easier? For example include this header in script file, so it will be treated as a script but also would be visible in our C++ project?

Regards
virious
One option is to use boost::preprocessor. Ex:

#include <boost/preprocessor.hpp>

#define ENUMS (alpha, (beta, (gamma, (delta, BOOST_PP_NIL))))

enum MyEnum {
BOOST_PP_LIST_ENUM(ENUMS)
};

// in you registration function
r = engine->RegisterEnum("MyEnum"); assert(r >= 0);
#define REGISTER_ENUM_VALUE(rv, data, elem) r = engine->RegisterEnumValue("MyEnum", #elem, elem); assert(r >= 0);
BOOST_PP_LIST_FOR_EACH(REGISTER_ENUM_VALUE, _, ENUMS)
#undef REGISTER_ENUM_VALUE
Advertisement
It would be possible to include a C/C++ header in AngelScript if you remove the incompatible stuff with preprocessor conditions.

However, an enum declared in the script isn't seen by the application until after the compilation so you wouldn't be able to use it in your application interface. Nor is it possible for a script to declare an enum that has already been declared by the application interface.


Probably the best way is to add a compilation step that will parse your C++ code to provide the registration of the application interface. If enums is your only concern I'm sure it wouldn't be difficult to write a parser to understand the enum declaration and write the code for registering the enum with AngelScript automatically.

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


It would be possible to include a C/C++ header in AngelScript if you remove the incompatible stuff with preprocessor conditions.

However, an enum declared in the script isn't seen by the application until after the compilation so you wouldn't be able to use it in your application interface. Nor is it possible for a script to declare an enum that has already been declared by the application interface.


Probably the best way is to add a compilation step that will parse your C++ code to provide the registration of the application interface. If enums is your only concern I'm sure it wouldn't be difficult to write a parser to understand the enum declaration and write the code for registering the enum with AngelScript automatically.


I wanted to be able to use enums declared in script also while registering my application interface. In int asCBuilder::RegisterEnum(asCScriptNode *node, asCScriptCode *file) you have code:
module->enumTypes.PushLast(st);
st->AddRef();
engine->classTypes.PushLast(st);

Shouldn't it be:
module->enumTypes.PushLast(st);
st->AddRef();
engine->classTypes.PushLast(st);
engine->objectTypes.PushLast(st);

or just:
module->enumTypes.PushLast(st);
st->AddRef();
engine->objectTypes.PushLast(st);
?

Regards
virious
Hmm. It does seem to be a bit inconsistent, doesn't it?

It isn't broken the way it is, so I won't make any changes now, but I'll have a look at this sometime in the near future.

Thanks for pointing it out to me.

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

I am using function WriteConfigToFile from the scripthelper add-on and I found another bug (or at least I think so, so if not, please correct me). I have some enums in script file. So they are not registered in the usual way (by RegisterEnum() on Angel Script engine), but they are added in earier mentioned method int asCBuilder::RegisterEnum(asCScriptNode *node, asCScriptCode *file).

When you call:
c = engine->GetEnumCount();

The c is 0, because asCBuilder is not adding enums to the engine->registeredEnums.

So I've added:
engine->registeredEnums.PushLast(st);

in the earlier mentioned place and it seems to be working ok now.

Thanks in advance.
Advertisement
The engine->registeredEnums array is only supposed to hold the enums registered by the application, so it's not to correct to change asCBuilder::RegisterEnum() to include the script declared enum in the registeredEnums list.

The WriteConfigToFile is only supposed to write what was actually registered by the application, not what has been built in the scripts.

If you wish to enumerate what has been built in the scripts you can do so by calling the methods on the module, e.g. asIScriptModule::GetEnumCount(), etc.

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

This topic is closed to new replies.

Advertisement