Registering a function that returns a pointer

Started by
2 comments, last by WitchLord 12 years, 10 months ago
Ok, so far i almost got all running with AS - thank you for that scripting library!




Now the only thing left i need to register, is a method in a class that returns a pointer.

To quickly overview:






struct bspEntity
{
CVector3 origin;
CVector3 color;
float radius;

std::string name;




bool castshadows;



};





bspEntity *XBSP::getEntity(const std::string entityName)
{
...
}







How do i call RegisterObjectMethod properly ? I have bspEntity already registered and tried to register class method as per below:









eScripting->RegisterObjectMethod("XBSP", "bspEntity getEntity(const string &in)", asMETHODPR(XBSP,getEntity, (const string &in), bspEntity*), asCALL_THISCALL);



That crashes the application, nothing in the report log.

Can i ask you guys for a little help with this please?
perfection.is.the.key
Advertisement
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

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


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..




perfection.is.the.key
Pointers are not supported in AngelScript, as there is no way of guaranteeing that they are valid at all times. References are supported under controlled circumstances.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

This topic is closed to new replies.

Advertisement