🎉 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 pass const objects?

Started by
1 comment, last by SharkBait 19 years, 2 months ago
I have a registered object entity (GameContext) that has getter and setter methods that accept a String type (registerd std::string). I had no problems passing non-const types. For example, the code: String URL_SITE = "http://localhost/sb"; String URL_GET_HISCORES = URL_SITE + "/get_hiscores.php"; void SetInternetUrls(GameContext @hGameContext) { hGameContext.SetUrlGetHiscores(URL_GET_HISCORES); hGameContext.SetUrlCanSubmitHiscore(URL_CAN_SUBMIT_HISCORE); hGameContext.SetUrlSubmitHiscore(URL_SUBMIT_HISCORE); } works fine. I would like hover to make GameContext's methods accept (const String) instead of (String). I first tried altering the two global variables as constants: const String URL_SITE = "http://localhost/sb"; const String URL_GET_HISCORES = URL_SITE + "/get_hiscores.php"; However, this caused an AngelScript compilation error stating that there are no methods with signatures that accept (const String&). I then went through my C++ GameContext class and the script wrapper functions and altered the methods and associated script wrapper functions to accept (const std::string &) but Angelscript does not allow me to register the functions. What's the correct way to work with consts? Is there something I need to add to GameContext and String's registered behaviours? P.S. const worked fine with native types. Regards,
tIDE Tile Map Editorhttp://tide.codeplex.com
Advertisement
Can you show us how you register std::string? I'd especially like to see the assignment behaviour. You must have one that accepts a (const string ∈).

The game context's methods is best registered as follows:

engine->RegisterObjectMethod("GameContext", "void SetUrlGetHiscores(const String ∈)", asMETHOD(GameContext,SetUrlGetHiscores), asCALL_THISCALL);


Note, that the in keyword is important, as without it the engine will give an error.

You may also take a look at the new ScriptString that I included as add_on in the latest release. It is basically a wrapper for std::string, that adds support for reference counting which allows handles to strings to be used in the scripts. It has also been prepared for use as const strings. The best thing about it is that this type can be passed to application functions that take a normal std::string by reference without any changes needed.

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

The 'in' bit was what I had missing in fact! Due to the way I wrapped angelscript into my own framework, I cannot show any (meaningful) code, but essentially I register script functions for each method and behaviour, for example:

// script-friendly 'method' function
void GameContext_SetUrlGetHiscores(
GameContext &thisObject, const string &strUrlGetHiscores)
{
thisObject.SetUrlGetHiscores(strUrlGetHiscores);
}

I then register this function using the asOBJFIRST calling convention. I know I can use the asMethodPtr(...) macros to register methods and the related overrides directly, but unfortunately I had trouble fitting it behind the facade of my framework.

Anyway, I was registering the above function using the signature:

"void SetUrlGetHiscores(const String &)"

which was giving me problems. Adding the 'in' keyword solved the problem:

"void SetUrlGetHiscores(const String ∈)"

Thanks!
tIDE Tile Map Editorhttp://tide.codeplex.com

This topic is closed to new replies.

Advertisement