🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Passing objects around

Started by
3 comments, last by WitchLord 17 years, 9 months ago
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]
I live in Palestine, but it's not in the country list, so I picked Zimbabwe.
Advertisement
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.
tIDE Tile Map Editorhttp://tide.codeplex.com
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?
I live in Palestine, but it's not in the country list, so I picked Zimbabwe.
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.
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.

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