Jump to content
  • Advertisement
Sign in to follow this  
FDsagizi

"?&in" to function?

This topic is 2235 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

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" ); Edited by FDsagizi

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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" );
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!