Skip to main content
GameDev.net gamedev.net
🔒 Locked

asIScriptGeneric with object method possible?

Started by ruphert Oct 20, 2012 at 9:18 PM 5 replies 2.3k views
Original Post
ruphert
ruphert
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( ?∈ )",
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/
SiCrane
SiCrane
Function pointers registered with asCALL_GENERIC must have the type void (*)(asIScriptGeneric *) this means that they cannot be member functions.
WitchLord
WitchLord
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(?∈)", 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 - <a href="http://www.angelcode.com/tower" rel
ruphert
ruphert
Thanks Andreas I suspected that worked. But I want to specifically use asIScriptGeneric* because this function will have variable number of parameters:
void KeepInBounds(?∈)
void KeepInBounds(?∈, ?∈)
void KeepInBounds(?∈, ?∈, ?∈)
etc.
KAG DEVLOG: http://kagdev.tumblr.com/
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel
ruphert
ruphert
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/
SiCrane
SiCrane
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);
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.