Advertisement

const char* returns and script string

Started by December 17, 2012 01:25 PM
1 comment, last by Starfox 12 years, 1 month ago
Can I bind a function that returns a char* in the script as one that returns a string? Here's the binding I'm using and it doesn't seem to be working:

Engine->RegisterObjectMethod("event", "string characters() const", asMETHODPR(event, characters, (void) const, const char* const), asCALL_THISCALL);

The function declaration is along the lines of:

class event
{
...
const char* const characters() const;
...
};

Do I have to wrap it somehow or can I hint AS to convert to a string? I'm using the string binding code that comes with the SDK.
Holy crap I started a blog - http://unobvious.typepad.com/
Unfortunately you'll need to create a wrapper for this.

There is no automatic conversion from const char * to std::string or vice versa.

You don't need to change your class to create the wrapper. You can implement a global function and register it with AngelScript as if it was a class member:


// Create a wrapper to convert const char * to std::string
std::string event_characters(const event &self)
{
return std::string(self.characters());
}

// Register the wrapper instead of the method
engine->RegisterObjectMethod("event", "string characters() const", asFUNCTIONPR(event_characters, (const event &), std::string), asCALL_CDECL_OBJLAST);


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

Advertisement
Great, thanks.
Holy crap I started a blog - http://unobvious.typepad.com/

This topic is closed to new replies.

Advertisement