Advertisement

Function which take pointer declaration

Started by September 08, 2005 03:40 PM
11 comments, last by DaesDemon 19 years, 2 months ago
Try this instead:

// DUMMY FUNCTION FOR ANGELSCRIPTS HANDLES void Addref(void*){}void Release(void*){}mASEngine->RegisterObjectBehaviour("Camera",asBEHAVE_ADDREF,"void f()",asFUNCTION(Addref),asCALL_CDECL_OBJLAST);mASEngine->RegisterObjectBehaviour("Camera",asBEHAVE_RELEASE,"void f()",asFUNCTION(Release),asCALL_CDECL_OBJLAST);


As for parameter references; The reason for the copy of the object instead of the true object is that this was the only way I found that I could guarantee the lifetime of the object during the call. I agree that it is quite confusing, and I do indeed plan on changing it somehow. I just can't figure out the best way to do it.

Right now, I think I'll just remove input parameter references altogether from the script language, as it doesn't make sense anyway. Output parameter references will still work as a way to have multiple return values. An application will still be able to register functions that take an parameter reference as input, but the script writer will see the functions as any other, and the script engine will do the conversion as necessary.

In a future version I may change the memory management system to move away from reference counting, and instead use true garbage collection. This could possible allow me to pass the true reference, but it may also introduce other problems. I need to analyze this thuroughly so that I don't introduce new security holes by a change like this.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Curiously,
void Addref(void*){}void Release(void*){}mASEngine->RegisterObjectBehaviour("Camera",asBEHAVE_ADDREF,"void f()",asFUNCTION(Addref),asCALL_CDECL_OBJFIRST);mASEngine->RegisterObjectBehaviour("Camera",asBEHAVE_RELEASE,"void f()",asFUNCTION(Release),asCALL_CDECL_OBJFIRST);

seems to work.

Advertisement
OOPs sorry, crossposting
Thank you WitchLord,
i think i have now a way to simulate pointer on angelscript
Great ;)

I had thought you had some kind of problems like you explain.
but was curious about why.
Quote:
guarantee the lifetime of the object during the call

So you mean you don't want to have the same problems that you could have if you stock reference on destroyed object in Cpp, is it?

For those who are interested, i will explain shortly how to be able to register Cpp function or methods that returns or takes as parameters pointer on object, or reference on object, but want the real object reference and not the copied one.
The goal is to be able to use handles that refers to the object itself but need the reference counting context.
1) Declare a dummy function:
Quote:
void DummyRefcount(void*) {}

2) Register for each of the classes where you need it theses two behaviours:
Quote:
engine->RegisterObjectBehaviour("Object",asBEHAVE_ADDREF,"void f()",asFUNCTION(DummyRefCount),asCALL_CDECL_OBJFIRST);engine->RegisterObjectBehaviour("Object",asBEHAVE_RELEASE,"void f()",asFUNCTION(DummyRefCount),asCALL_CDECL_OBJFIRST);

3) Use Handles in the AngelScript declaration instead of reference like:
Quote:
Engine->RegisterObjectMethod("Engine","void destroyCamera(Camera@ camera)",asMETHOD(Engine,destroyCamera),asCALL_THISCALL);

4) In Angelscript you will be able to use it like this:
if engine is an Engine and myCamera is a Camera
Quote:
engine.destroyCamera(myCamera);

This refer to a method like this one in Cpp:
Quote:
class Engine{    ....    void destroyCamera(Camera* camera) {delete camera;} // for example    ....}

But be careful as the reference counting is a fake, if you destroy the CameraObject in destroyCamera() , myCamera will not be valid anymore in the script so access to it will cause crash.

Thanks to WitchLord and RainDog for their help.

This topic is closed to new replies.

Advertisement