Advertisement

"?&in" to function?

Started by May 04, 2012 06:16 AM
2 comments, last by WitchLord 12 years, 6 months ago
Hello! smile.png

So, a create GUI in game engine, and need use callback's

cur time i use this code:


class SomeClass
{
void MyCallbackFuciton() {}

void Load()
{
GetWidget( "Start" ).AddEvent( "MyCallbackFuciton" );
}
}

c++:

r = en->RegisterObjectMethod(type,"void AddEvent( const string &)",asMETHOD( T, AddEvent), asCALL_THISCALL ); assert( r >= 0 );

void AddEvent( const std::string &_method )
{
asIScriptContext *ctx = asGetActiveContext();
asIObjectType *obj_type = ctx->GetEngine()->GetObjectTypeById( ctx->GetThisTypeId() );

// get method by name
funcId = obj_type->GetMethodByName( method.c_str() );
}


whether it is possible to make so?



class SomeClass
{

void MyCallbackFuciton() {}

void Load()
{
GetWidget( "Start" ).AddEvent( MyCallbackFuciton );
}

}

c++:

r = en->RegisterObjectMethod(type,"void AddEvent( func_id or int or asIScriptFunction or.. )",asMETHOD( T, AddEvent), asCALL_THISCALL ); assert( r >= 0 );

void AddEvent( int _funct_id or asIScriptFunction *_fucnt )
{
// ...
}


all sense in checking correctness at a compilation stage

i.e. this code work, but no correct GetWidget( "Start" ).AddEvent( "MyCallbackFuciton_ololo" );

Hello! smile.png

So, a create GUI in game engine, and need use callback's

cur time i use this code:


class SomeClass
{
void MyCallbackFuciton() {}

void Load()
{
GetWidget( "Start" ).AddEvent( "MyCallbackFuciton" );
}
}

c++:

r = en->RegisterObjectMethod(type,"void AddEvent( const string &)",asMETHOD( T, AddEvent), asCALL_THISCALL ); assert( r >= 0 );

void AddEvent( const std::string &_method )
{
asIScriptContext *ctx = asGetActiveContext();
asIObjectType *obj_type = ctx->GetEngine()->GetObjectTypeById( ctx->GetThisTypeId() );

// get method by name
funcId = obj_type->GetMethodByName( method.c_str() );
}


whether it is possible to make so?



class SomeClass
{

void MyCallbackFuciton() {}

void Load()
{
GetWidget( "Start" ).AddEvent( MyCallbackFuciton );
}

}

c++:

r = en->RegisterObjectMethod(type,"void AddEvent( func_id or int or asIScriptFunction or.. )",asMETHOD( T, AddEvent), asCALL_THISCALL ); assert( r >= 0 );

void AddEvent( int _funct_id or asIScriptFunction *_fucnt )
{
// ...
}


all sense in checking correctness at a compilation stage

i.e. this code work, but no correct GetWidget( "Start" ).AddEvent( "MyCallbackFuciton_ololo" );


i asked the same question last night :)

check my post.

not as easy as passing a function pointer.
Advertisement

i asked the same question last night

check my post.

not as easy as passing a function pointer.



It would be very convenient!
Let's wait for the new version!smile.png
Without support for delegates in the language (on to-do list), the best solution is to use interfaces. Interfaces will provide the proper compile time checks that you're looking for.

Example:



// This interface should be registered by the application
interface IWidgetEventHandler
{
void Callback();
}


class SomeClass : IWidgetEventHandler
{
// The class is guaranteed to implement the Callback method, since it implements the inteface
void Callback() {}

void Load()
{
// The AddEvent method takes a handle to IWidgetEventHandler
GetWidget( "Start" ).AddEvent( this );
}
}

//c++:

r = en->RegisterObjectMethod(type,"void AddEvent(IWidgetEventHandler @+)",asMETHOD( T, AddEvent), asCALL_THISCALL ); assert( r >= 0 );

void AddEvent( asIScriptObject *obj )
{
asIObjectType *obj_type = obj->GetObjectType();

// The object type is guaranteed to be a IWidgetEventHandler
funcId = obj_type->GetMethodByName( "Callback" );
}

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