Jump to content
  • Advertisement
Sign in to follow this  
virious

C++ header file shared with scripts

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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

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

Thanks in advance.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!