Advertisement

Quick hint on setters and getters defined from C

Started by January 09, 2010 03:02 AM
4 comments, last by jal_ 15 years, 1 month ago
Hi, Can you give me a quick hint on how to setup setters and getters in a class defined from C? I've been roaming the tutorials but all I found are examples defined from angelscript. I know it can't be very hard, but a quick hint would come handy. Thanks in advance.
You need to register the class methods using the prefixes 'get_' and 'set_', e.g.

r = engine->RegisterObjectMethod("myObj", "int get_myProp() const", asMETHOD(myObj, get_myProp), asCALL_THISCALL); assert( r >= 0 );r = engine->RegisterObjectMethod("myObj", "void set_myProp(int)", asMETHOD(myObj, set_myProp), asCALL_THISCALL); assert( r >= 0 );


The text in the manual can definitely be improved to explain this better.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
I'm getting a problem here. I'm updating to 2.17.2 from 2.16.x (I know it's old already, but I chose updating step by step), and the objects I factor for the getters are never released. This is how I'm declaring it:

The property:
{ SCRIPT_PROPERTY_DECL(cString @, name), FOFFSET(game_locals_t, name) },

The method:
{ SCRIPT_FUNCTION_DECL(cString &, get_name, ()), objectGame_GetName, objectGame_asGeneric_GetName, asCALL_CDECL_OBJLAST },

The callback
static asstring_t *objectGame_GetName( game_locals_t *self ){	return objectString_FactoryBuffer( self->name, strlen( self->name ) );}static asstring_t *objectString_FactoryBuffer( const char *buffer, unsigned int length ){	asstring_t *object;	object = objectString_Alloc();	object->buffer = G_AsMalloc( sizeof( char ) * ( length + 1 ) );	object->len = length;	object->buffer[length] = 0;	object->size = length + 1;	if( buffer )		Q_strncpyz( object->buffer, buffer, object->size );	return object;}static inline asstring_t *objectString_Alloc( void ){	static asstring_t *object;	object = G_AsMalloc( sizeof( asstring_t ) );	object->asRefCount = 1;	object->asFactored = 1;	asstring_factored_count++;	return object;}


I'm just wondering if there was some known issue at 2.17 regarding this, or if there was something changed on the reference counting from 2.16 to 2.17 I am missing.




To AngelScript there is a difference in returning a handle and a reference. AngelScript will release the handle when it is done with it, but for the reference nothing will be done.

It looks like you're declaring the get accessor to return a reference, but in reality you want it to return a handle.

There is nothing on the Change list regarding the class property getters and setters since 2.17.2. I did however add support for getters and setters for global properties in 2.18.1, as well as adding a lot of optimizations that you may find interesting.

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

I tried first to declare the property and the accesor returning the same thing, but I don't get it to build. It either drops with asINVALID_DECLARATION on the property ( "cString & name" ) or with "No matching operator that takes the types 'cString &' and 'cString @' found" when I use handles on the accesors. Maybe my real problem is at incorrectly using references at the conversion of the behaviors to methods. I'm going to recheck that.

EDIT: I do intend to continue updating to 2.18. It just happens that our implementation is pretty big, so updating less changes at once makes it easier. At least I prefer it that way.
Got it sorted out. It turned out I just had to set the return of the new operator methods to have autohandle. Thanks again.

This topic is closed to new replies.

Advertisement