Sign in to follow this  
adelamro

Passing objects around

Recommended Posts

adelamro    122
Hi. I've been experimenting with AngelScript, and there is something I can't figure out. Is there a way to pass objects around by reference without having to add AddRef and Release functionality? I mean, I don't want to rewrite all my classes. Also, consider this: engine->RegisterGlobalFunction( "void DoSomething( CObject& in )", asFUNCTION( DoSomething ), asCALL_CDECL ); If you register a global variable of type CObject and then in a script pass it to a function like DoSomething, you'd expect it would receive the address of the variable you registered, but you don't. Apparently, using & in AngelScript causes the object to be copied, and the function gets called on the copy, and then the copy is copied back to the original parameter. So it's pretty much useless. The way around this, as I see it, is to register dummy AddRef and Release functions for each type. However, since AngelScript only calls Release and not the destructor, you can't allow the script to create items of this type except through a Create function which returns a handle, and the script will also have to call a Delete function on these handles. What a pain! Is there anything I'm missing here? [Edited by - adelamro on August 18, 2006 5:41:21 AM]

Share this post


Link to post
Share on other sites
SharkBait    158
With some good thinking, you can add AddRef/Release behaviour by binding a pair of global functions and leaving your classes intact. However, you will need a way to store reference counts for every object instance, perhaps via an associative container to map object pointers to a reference count. This way you can register the same pair of functions for all classes and still keep track of reference counts for all object instances regardless of their class.

The reason for the 'unnecessary' copying of & references is due object safety. I'm not sure about this, but I believe you can actually turn the copying off by adding a pre-processor definition to allow unsafe references. I think this particular issue has been debated a million times, and I for one contested it until I understood the philosophy behind it :)

Anyway, the closest and safest thing to using 'fast' pointers is still to use @ references. The reason for the reference count behaviour was to let the scripting engine treat object references much like those treated in a managed langage like .NET or Java. These languages use a garbage collector to clean up discarded objects. Angelscript uses a reference count mechanism to achieve the same thing without resorting to a background garbage collection process.

Share this post


Link to post
Share on other sites
adelamro    122
OOOOOOH! It's working!!! Although I knew about the AS_ALLOW_UNSAFE_REFERENCES flag, I couldn't get it to work earlier the way I wanted because I did not realize the importance of specifying in/out. As it turns out, you need to use somethingn like


engine->RegisterGlobalFunction( NULL, "void Function( CObject& inout )", ... );


in order to have the library send a reference to the original object. This is described in the docs in User manual > Datatypes in AngelScript and C++. Maybe it should've been in the section about object handles? Anyway, thanks so much, SharkBait.

One last note: I spent more than an hour studying test_pointer.cpp in the tests that came with AngelScript, comparing my code to it and desperately trying to figure out what I was doing wrong. Finally I found out that that test was no longer valid. I almost cursed. If it's no longer valid, why is it included in the library?

Share this post


Link to post
Share on other sites
Deyja    920
No, using AS_ALLOW_UNSAFE_REFERENCES means you DON'T need the in/out keywords. You probably just defined it, then included angelscript.h, yeah? Well, that's the problem. You need to define it in as_config.h, and recompile the whole library. I have no trouble at all using it, and don't have any ins or outs or inouts anywhere.

Share this post


Link to post
Share on other sites
WitchLord    4677
Quote:
Original post by adelamro
One last note: I spent more than an hour studying test_pointer.cpp in the tests that came with AngelScript, comparing my code to it and desperately trying to figure out what I was doing wrong. Finally I found out that that test was no longer valid. I almost cursed. If it's no longer valid, why is it included in the library?


I plan on adding support for pointers again in the future, that's why I haven't removed the test.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this