Jump to content

  • Log In with Google      Sign In   
  • Create Account


TrickyLogic

Member Since 24 Sep 2011
Offline Last Active Apr 23 2013 10:38 AM
-----

Topics I've Started

Passing an Opaque Pointer to AngelScript

24 September 2011 - 05:11 AM

Here is what I would like to do:
  • pass an opaque pointer (OP) from C++ to a function written in AngelScript (AS)
  • not allow AS to keep a copy around after the function has returned
  • allow AS to pass OP around to other functions (either written in C++ and registered in AS or written in AS)
  • use different types for different OPs, AS must not be able to pass the wrong OP to a function!
The reasons for doing it this way are:
  • to decouple any data stored in the OP from AS as much as possible (AS must not have any knowledge about OP's implementation details)
  • to keep any interfacing with AS very basic and simple (no need for any "tricky" reference counting, just passing a pointer around)
  • to avoid overhead from garbage collecting (AS will only keep OP around for the duration of the function call)
Here is my attempt but as I'm rather new to AS and after some trial and error I still didn't succeed (hence this post :wink:). Code snippet for C++:
class SomeData{
public:
	void release(){
	}
};

void print_some_data( SomeData &some_data ){
	cout << "SomeData!" << endl;
}

/* ... */
	r = engine->RegisterObjectType( "SomeData", 0, asOBJ_REF | asOBJ_SCOPED ); assert( r >= 0 );
	r = engine->RegisterGlobalFunction( "void print_some_data( const SomeData &in )", asFUNCTION( print_some_data ), asCALL_CDECL ); assert( r >= 0 );
	r = engine->RegisterObjectBehaviour( "SomeData", asBEHAVE_RELEASE, "void f(  )", asMETHOD( SomeData, release ), asCALL_THISCALL ); assert( r >= 0 );

Code for AS (test.as):
void main( SomeData some_data ){
	print_some_data( some_data );
}

I get the error. "test.as (1, 11) : ERR : Parameter type can't be 'SomeData'". I think it is because AS wants to create a new object SomeData but does not know how. However, I just want AS to keep the OP around and not do anything with it, except pass it to some functions.

Is what I want to do possible in AS and if so how? It seems like an incredibly simple thing to do yet I can't figure it out. Any help will be greatly appreciated!

PARTNERS