Sign in to follow this  
xynapse

Registering a function that returns a pointer

Recommended Posts

xynapse    151
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:




[code]

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

std::string name;




bool castshadows;



};





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


[/code]




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







[code]

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

[/code]

That crashes the application, nothing in the report log.

Can i ask you guys for a little help with this please?

Share this post


Link to post
Share on other sites
WitchLord    4677
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:

[code]
// 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);
[/code]

Regards,
Andreas

Share this post


Link to post
Share on other sites
xynapse    151
[quote name='WitchLord' timestamp='1306248584' post='4815137']
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:

[code]
// 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);
[/code]

Regards,
Andreas
[/quote]




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:


[code]


/*
* 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);



[/code]





Well, that works too as a METHODPR

- so more likely, instead of returning a pointer - a pointer as an argument does the trick..




Share this post


Link to post
Share on other sites
WitchLord    4677
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this