asIScriptGeneric with object method possible?

Started by
5 comments, last by SiCrane 11 years, 5 months ago
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?
KAG DEVLOG: http://kagdev.tumblr.com/
Advertisement
Function pointers registered with asCALL_GENERIC must have the type void (*)(asIScriptGeneric *) this means that they cannot be member functions.
You're confusing the concepts :)

The asIScriptGeneric is used for the generic calling convention, which is for when native calling conventions are not supported

The variable type, ?&, 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:


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


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

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.
KAG DEVLOG: http://kagdev.tumblr.com/
Then the function must be implemented like this:


// 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
}
}


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

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?
KAG DEVLOG: http://kagdev.tumblr.com/
You can just register a proxy function if you want that behavior. Something like:

void generic_proxy(asIScriptGeneric * gen) {
CObject * self = static_cast<CObject *>(gen->GetObject());
self->MyGenericFunction(gen);
}

This topic is closed to new replies.

Advertisement