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

A Question or probably Suggestion

Started by
2 comments, last by WitchLord 18 years, 8 months ago
I was just able to implement angelscript and see some samples, just starting and checked the documentation. Anyways I've been wondering if object handles may be type casted into int and viceversa, it seems there isn't direct way to do so, but would like to know if there is a way to do so with a function. [Edited by - Vexorian on October 14, 2005 4:47:56 PM]
------ XYE - A new edition of the classic Kye
Advertisement
AngelScript doesn't provide such a function for you, since it could be potentially disastrous to allow the conversion of arbitrary int's to object handles.

However, there is nothing stopping the application developer from registering a function that takes an int as parameter and returns an object handle.

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 then is there a way to register a function to take any object handle as argument?

It seems not, I think that you could make a read only cast between object handles and ints so you can get the int but you can't make an integer back into an object handle.

Although I solved the problem where I needed this behaviour, the first half of the problem was solved by the any type which is great btw. And for the second half of the problem I decided to give all the objects in the game an id unsigned int property that is unique unless you had an overflow or something (32 bits make really high numbers though=) .

But I have some concerns:
    r = engine->RegisterObjectMethod("table","bool SetInt(string@,int)",asMETHOD(aslt,LinkInt),asCALL_THISCALL); assert( r >= 0 );    r = engine->RegisterObjectMethod("table","const int Int(string@)",asMETHOD(aslt,GetLinkedInt),asCALL_THISCALL);  assert( r >= 0 );    r = engine->RegisterObjectMethod("table","bool SaveRef(string@,any@)",asMETHOD(aslt,LinkAny),asCALL_THISCALL); assert( r >= 0 );    r = engine->RegisterObjectMethod("table","const void RestoreRef(string@,any@&out)",asMETHOD(aslt,SetLinkedAny),asCALL_THISCALL);  assert( r >= 0 );    r = engine->RegisterObjectMethod("table","bool SetTable(string@,table@)",asMETHOD(aslt,LinkTable),asCALL_THISCALL); assert( r >= 0 );    r = engine->RegisterObjectMethod("table","table@ &Table(string@)",asMETHOD(aslt,GetLinkedTable),asCALL_THISCALL);  assert( r >= 0 );


First of all I have no clue about what the & operator means when it is used before a function's identifier, I've seen it being used and when registering things I noted that sometimes not using it caused access violations and some times not using it caused them.

Second I am not sure if a cpp is supposed to save a ref for an asIScriptAny handle, use AddRef() and let some other method return it later, I don't know if that's safe, in case it isn't is there another way to store an any and use it later?

Third, let me see if I get this straight:
- A function/method/constructor that creates a new object should make its refcount to 1.
- A function that is saving an object's ref for later use should increase it to 1.

- And any function that returns an objects ref should also use AddRef(), this is the part I don't really understand, if I previously added a ref when saving it? Am I also supposed to increment it when returning it again?
------ XYE - A new edition of the classic Kye
I'm glad you found the any type useful. I was going to suggest you use it when I read your first sentences. :)

The & before the function name means that the function returns a reference to the return type. This is the same as for C++. Also, only application registered functions are permitted to return references at this moment.

The application can save a reference to the asIScriptAny, and should use the AddRef()/Release() methods to control how many references are being held.

Ideally the constructor for the object that supports object handles should initialize the reference count to 1.

When the application returns an object handle it should make sure that the returned handle reference is accounted for.

When the application receives an object handle in a parameter, and don't want to store it, it must release the reference before returning from the function.

Here's a couple of examples to illustrate this:

// obj@ func1()obj *func1(){  obj *o = new obj();   // 1 reference is already accounted for in the constructor  return o;}obj *g2 = 0;// obj@ func2()obj *func2(){  if( g2 ) g2->Release();  g2 = new obj();   // Since the application keeps a handle and   // also returns one we must increase the refcount  g2->AddRef();  return g2;}// void func3(obj@)void func3(obj *o){  // We must release the handle that we received  if( o ) o->Release();}// obj@ func4(obj@)obj *func4(obj *o){  // Since we return the reference we received   // we don't need to adjust the ref count  return o;}


You should use the above for functions written specifically for AngelScript. But if you have functions not already prepared for AngelScript that you still want to be able to work with handles, you can in certain conditions use AngelScript's autohandles. By registering a function with the autohandles AngelScript can automatically release handles passed in parameters, and add references to handles returned.

// AngelScript automatically increases the reference for the handle returnedengine->RegisterGlobalFunction("obj @+ func()", asFUNCTION(func), asCALL_CDECL);// AngelScript automatically releases the reference once the function returnsengine->RegisterGlobalFunction("void func(obj @+)", asFUNCTION(func), 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