Advertisement

question about registering global functions

Started by May 30, 2008 01:46 PM
2 comments, last by WitchLord 16 years, 5 months ago
I am still a complete idiot in angelscript, but I am wondering if I can do something like the following example. In c++, let's say I have a method as follows: typeA* GetA( int param ); I am wondering if I can directly register this function in angelscript so it returns a typeA& as in the following: typeA& GetA( int param ); Our game engine interface currently makes heavy use of of the idea of returning pointers to objects and it would be nice to not have to write a translation layer between the interface and the script. The only alternative right now is to write a wrapper function.
Yes, this perfectly possible without any wrapper function, because in C++ a pointer is the same as a reference. With just a few more restrictions for references.

You just have to be careful to make sure the function doesn't return a null pointer as AngelScript will raise a null-pointer exception if that happens. Also, AngelScript will not make any attempts to deallocate the returned address, so don't use this way to allocate memory unless you also store the pointer in the application for later deallocation.

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
Yes all dynamic memory allocation/deallocation happens in the application and the script just accesses methods and variables from any pointer it is using.

I haven't had a chance to try switching from the application->script layer we are currently using to just using references, but it sounds like for the most part everything will work. There are however, a few instances where it is valid for a function or method to return a null pointer. For instance, I might use the following line of code in the script:

device.SetTexture( entity.GetTexture() ) where GetTexture returns a Texture* which is registered as a Texture& in angelscript. If GetTexture is a null pointer, this indicates to the device that it should not texture the entity, but angelscript would get a null exception error.

Is it possible to flag angelscript to throw null exceptions for a certain type only when there is an attempt to invoke a particular method or member on it?
If you need to be able to return a null pointer, then ideally you would register the Texture as a reference type with support for reference counting. That way you could register the GetTexture function to return an object handle instead of a reference.

// Register the Texture type with reference countingRegisterObjectType("Texture", 0, asOBJ_REF);RegisterObjectBehaviour("Texture", asBEHAVE_ADDREF, asMETHOD(Texture,AddRef), asCALL_THISCALL);RegisterObjectBehaviour("Texture", asBEHAVE_ADDREF, asMETHOD(Texture,AddRef), asCALL_THISCALL);// Use auto handle (@+) as the function doesn't // increment the reference counter itselfRegisterGlobalFunction("Texture@+ GetTexture()", asFUNCTION(GetTexture), asCALL_CDECL);





If your Texture class currently isn't reference counted, and you don't want to add that support, then you could register a value type that will hold the pointer value. The only problem with this is that you won't have any control of which texture pointers that the script holds.

// Register a type that can hold the texture pointerRegisterObjectType("TexturePtr", sizeof(Texture*), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);// Register the function to return a TexturePtr type, // though in reality it is a Texture*RegisterGlobalFunction("TexturePtr GetTexture()", asFUNCTION(GetTexture), asCALL_CDECL);

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