🎉 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!

Handles as return values

Started by
1 comment, last by WitchLord 19 years, 2 months ago
Hi! I have a question about script functions allocating objects and returning handles. I'd like to do something like this:

Test1@ foo() {
   Test1 t;
   return @t;
}
However, it seems that Test1's constructor is not called when creating the object, and it is deallocated when exiting the function (when I retreive the return value, it's not a valid pointer) Shouldn't 'return @t' cause AddRef to be called? I have also tried:

Test1@ foo() {
   Test1 t;
   Test1@ t2;
   @t2 = @t;
   return @t2;
}
But, the same thing happens: AddRef is called twice, then Release is called twice and return value is not a valid pointer. Am I doing something wrong, or is this a bug/not implemented? On a side note: Wouldn't it be more natural to work with handles the same way pointers are handled in C/C++? The last script would then look like this:

Test1@ foo() {
   Test1 t;
   Test1@ t2;
   t2 = @t; // since t2 is a handle (pointer)
   return t2;
}
Of course, we'd need a dereference operator: *t2 = t;
Advertisement
Ok, I should've read the overview a bit more carefully :)

I forgot to register constructor/destructor behaviour...
I assume that everything works ok now.

I chose to do the syntax for the handles as I did since I think it is more consistent. It was a consious choice to go against the syntax of pointers in C++. Basically an object handle behaves the exact same way as the real object until you explicitly tell the compiler you wish to do a handle operation. This means that:

obj o;obj@ h;h = @o;


Would try to assign the handle of o to the object that h refers to.

Also, with this way of working with the handles, there is no need for a dereferencing operator (*), not the indirect member access operator (->).

Still, I'm currently making the compiler do implicit conversion to handles where it doesn't lead to ambiguities. For example:

@h = @o;h = @o;@h = o;


All these three statements will work, and will have the exact same effect.

Passing parameters by handle will be equally easy. No longer will there be a need to add the @ before the argument to pass it's handle, the compiler will implicitly do it for you.

I will have this completed for 2.2.0 WIP 2.

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

This topic is closed to new replies.

Advertisement