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

pass by handle to C++ question..

Started by
4 comments, last by AlphaDog 19 years, 1 month ago
I'm pretty new to AngelScript, but I really like what I see and I am finding it very easy to use.. But I have one question. I need to pass an object by its handle to a C++ function. I have the object registered with: asOBJ_CLASS | asOBJ_CLASS_CONSTRUCTOR | asOBJ_CLASS_DESTRUCTOR| asOBJ_CLASS_ASSIGNMENT and the following behaviors defined: asBEHAVE_ADDREF,asBEHAVE_RELEASE,asBEHAVE_DESTRUCT,asBEHAVE_CONSTRUCT,asBEHAVE_ASSIGNMENT If I want to pass this object as a pointer ( handle ) back to a C++ function I would do the following in AngelScript: Object obj; dumpObj(@obj); and the C++ function looks like this: void dumpObj( Object *pObj) { std::cout << (*pObj) << std::endl; } I may be missing something simple, does the AngelScript code above create a dangling handle that does not get released. I think I'm missing something really obvious... Thanks, Scott
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'
Advertisement
Hi Scott,

I'm pleased that you like AngelScript. Let me know if there is something you think can be improved.

You don't need to register the asBEHAVE_DESTRUCT behaviour if you register asBEHAVE_ADDREF and asBEHAVE_RELEASE, because the library will never call the destructor. It will just call the release behaviour and expect that method to free the object when no more references are held.

Your C++ function must release the handle before returning, as AngelScript call the addref behaviour to account for the argument. If you don't call the release behaviour method on the pointer, you will have a memory leak.

void dumpObj( Object *pObj){  std::cout << (*pObj) << std::endl;  pObj->Release();}


If your C++ function returns a handle to the script, then the library assumes that the function increases the reference counter before returning. Example:

Object *pGlobalObj;Object *GetGlobalObject(){  pGlobalObj->AddRef();  return pGlobalObj;}


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

Ok.. That makes sense..

The asBEHAVE_DESTRUCT behavior is kind of a leftover. I have a simple sandbox app and I'm testing if AngelScript has all the functionality I need, so I just keep changing the code trying different features.

I have a large plugin based object system that uses DLLs to store objects and implements Scott Bilas' FuBi concept: http://www.drizzle.com/~scottb/gdc/fubi-paper.htm

I had been using Lua and had everything working really well. I could drop a DLL into the directory and the system would pick it up and new objects would be available immediately in script with zero coding. It was pretty clean. Problem was the Lua syntax; I just couldn't get use to it, neither could any of the art team. Don't get me wrong, Lua is powerful and well thought out, but I just don't like it's flavor.. like broccoli :)

AngelScript's syntax was just so comfortable I had to give it a closer look. I was amazed how easy it was to integrate into a class and how quickly I was up and running; of course some creadit has to go to Lua for making me learn so much about embedding a script language. AngelScript almost makes me feel lazy compared to Lua.

It is a bit early for me to suggest improvements, but I would love to see some form of excpetion handling. I know I used the Lua alert hook a lot to bind my C++ object system error handling with the script system. It made error reporting very easy..

I'm sure I'll have more questions down the line.. Thanks again for the quick response and the elegant scripting lanquage..

Cheers,
Scott
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'
There is exception handling, just not in script handling.
I read Scott's FuBi paper, it's actually one of the reasons that got me going on AngelScript in the first place. Lua is another of my inspirations, though I too don't like the syntax too much.

There is no try-catch statements in AngelScript yet, but exceptions are available and you can get pretty detailed information about when and where they occured.

If you set the exception callback function you can obtain information about the callstack when an exception occurs, otherwise you'll only be able to find location in the code where it occurred.

In a future version I'll even implemente try-catch statements so that the scripts can treat the exceptions if so desired.

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

Yea, I'm learning a lot more from the tests source about error handling and features.

~Scott
[size=1]'Behold! It is not over unknown seas but back over well-known years that your quest must go; back to the bright strange things of infancy and the quick sun-drenched glimpses of magic that old scenes brought to wide young eyes.'

This topic is closed to new replies.

Advertisement