# C++ header file shared with scripts

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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 

##### Share on other sites
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.

##### Share on other sites

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

##### Share on other sites
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.

##### Share on other sites
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.

engine->registeredEnums.PushLast(st);

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

##### Share on other sites
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

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013390
×