You get a crash because you're telling AngelScript that the bspEntity is returned by value, but you are really returning a pointer.
In your case you should be able to register the method as returning a reference to a bspEntity, as this has the same signature. The only problem with that is if the method can in some valid situation return a null-pointer. A reference can't be null, so you would get a null-pointer exception if that happens.
To handle this I suggest you write a tiny wrapper like this:
// This wrapper is able to safely handle the situation when the requested bspEntity doesn't exist
bool XBSP_getEntity(XBSP *bsp, const std::string &name, bspEntity &ret)
{
bspEntity *b = bsp->getEntity(name);
if( b )
{
ret = *b;
return true;
}
// entity doesn't exist
return false;
}
// Register the wrapper as if it was a method of XBSP
engine->RegisterObjectMethod("XBSP", "bool getEntity(const string &in, bspEntity &out)", asFUNCTION(XBSP_getEntity), asCALL_CDECL_OBJFIRST);
Regards,
Andreas
Ok, your solution works great, but instead of doing 'wrapper' method - any chance so i could make it my way - i understand there can be a chance that i'll get a NULL here, but just thinking how can i register that with AngelScript ?
Edit:
/*
* Wrapper
*/
bool XBSP::XS_getEntity(const std::string &name, bspEntity &ret)
{
bspEntity *b = getEntity(name);
if( b )
{
ret = *b;
return true;
}
// entity doesn't exist
return false;
}
eScripting->RegisterObjectMethod("XBSP", "bool getEntity(const string &in, bspEntity &out)", asMETHODPR(XBSP,XS_getEntity, (const std::string &in,bspEntity &out), bool), asCALL_THISCALL);
Well, that works too as a METHODPR
- so more likely, instead of returning a pointer - a pointer as an argument does the trick..