• Advertisement
Sign in to follow this  

asIScriptGeneric with object method possible?

This topic is 1904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to register a variable parameter method like in this example: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_adv_var_type.html
but in an object.

r = engine->RegisterObjectMethod("JGizmo", "void KeepInBounds( ?&in )",
asMETHOD( JGizmo, call),
asCALL_GENERIC); assert( r >= 0 );

but it fails to register with code: asWRONG_CALLING_CONV

with asCALL_THISCALL it just crashes when accessing the asIScriptGeneric object.

Is this possible at all for object methods?

Share this post


Link to post
Share on other sites
Advertisement
Function pointers registered with asCALL_GENERIC must have the type [tt]void (*)(asIScriptGeneric *)[/tt] this means that they cannot be member functions.

Share this post


Link to post
Share on other sites
You're confusing the concepts :)

The asIScriptGeneric is used for [url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_generic.html"]the generic calling convention[/url], which is for when native calling conventions are not supported

[url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_adv_var_type.html"]The variable type, ?&[/url], is used to pass any argument type to a function, and yes they can be used with object methods too.

In your case the object method should be implemented like this:

[code]
void JGizmo::KeepInBounds( void *ptr. int typeId )
{
// Determine the type of the argument with the typeId, and then cast the ptr accordingly with reinterpret_cast.
}

// Register it as
engine->RegisterObjectMethod("JGizmo", "void KeepInBounds(?&in)", asMETHOD(JGizmo, KeepInBounds), asCALL_THISCALL);
[/code]

Regards,
Andreas

Share this post


Link to post
Share on other sites
Thanks Andreas I suspected that worked. But I want to specifically use asIScriptGeneric* because this function will have variable number of parameters:
void KeepInBounds(?&in)
void KeepInBounds(?&in, ?&in)
void KeepInBounds(?&in, ?&in, ?&in)
etc.

Share this post


Link to post
Share on other sites
Then the function must be implemented like this:

[code]
// Global function, or static class method
void MyGenericFunction(asIScriptGeneric *gen)
{
for( asUINT n = 0; n < gen->getArgCount(); n++ )
{
int type = gen->GetArgTypeId(n);
void *ptr = gen->GetAddressOfArg(n);

... reinterpret_cast the pointer according to its type
}
}
[/code]

Regards,
Andreas

Share this post


Link to post
Share on other sites
Thanks this helped.
Still I would find it super useful to have generic parameters in objects like:

void CObject::MyGenericFunction(asIScriptGeneric *gen)

why can't that work? Any chance of it happening?

Share this post


Link to post
Share on other sites
You can just register a proxy function if you want that behavior. Something like:
[code]
void generic_proxy(asIScriptGeneric * gen) {
CObject * self = static_cast<CObject *>(gen->GetObject());
self->MyGenericFunction(gen);
}
[/code]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement