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

Please help with 2 problems

Started by
4 comments, last by WitchLord 19 years, 3 months ago
1st problem is how to register class functions like: class cClass { void func(cClass* somePointer) { param = somePointer; } private: cClass *param; }; how to register in AS? i've try to register like as: "void func(cClass&)",asMETHOD(cClass,func) but nothing works and 2nd problem is how to create function with pointer input,like in AS code: void OnUpdate(cClass@ pointClass) { // some changes to pointClass } Thanks
Advertisement
To register a method that takes a reference you must use the in, out, or inout reference flags, e.g:

RegisterClassMethod("cClass", "void func(cClass ∈)", asMETHOD(cClass, func), asCALL_THISCALL);

The problem with references is that in order guarantee the lifetime of the object the library takes the liberty of making copies of the objects, so the object you will receive in the pointer will not be the real object, but only a copy of it. Also once the function returns the library will destroy this copy, which also means that if you have stored the pointer it will no longer be valid.

If you need to store the pointer you receive then you have to register the function to receive the object by handle, e.g:

RegisterClassMethod("cClass", "void func(cClass@)", asMETHOD(cClass, func), asCALL_THISCALL);

In this case you'll also have to remember to update the reference counter appropriatly, i.e:

void func(cClass* someHandle){  // Add a reference when storing the pointer  someHandle->AddRef();  param = someHandle;  // Release the reference received from the script library  someHandle->Release();  // NOTE: In this case the AddRef() and Release() calls   // obviously take out each other, so you didn't really   // have to add them. But to illustrate how the library  // works, I added them anyway.}


In order to allow the use of handles in the script you need to register the asBEHAVE_ADDREF and asBEHAVE_RELEASE behaviours.

Let me know if you need further help.

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

Still unable to register class function, who needs to recieve pointer:
class cClass
{
public:
void func(cClass* classP)
{
classP->add(i);
}
void add(int x)
{
i += x;
}

private:
int i;
}

function add(int x) normally registered, but func(cClass*) - not
I register it via:
"void func(cClass@)",asMETHOD(cClass,func));
but there is unknown error

Any variants?

Thanks
Did you register the ADDREF and RELEASE behaviours for the class type before registering the function? The engine doesn't allow the use of object handles unless these behaviours are registered.

engine->RegisterObjectType("cClass", ...);engine->RegisterObjectBehaviour("cClass", asBEHAVE_ADDREF, "void f()", asMETHOD(cClass, AddRef), asCALL_THISCALL);engine->RegisterObjectBehaviour("cClass", asBEHAVE_RELEASE, "void f()", asMETHOD(cClass, Release), asCALL_THISCALL);// Register any other behaviours necessary for this class// Now we can register the object method with parameters using object handlesengine->RegisterObjectMethod("cClass", "void func(cClass@)", asMETHOD(cClass, func), asCALL_THISCALL);


Take a look at the test code in test_objhandle.cpp for a working example of how to use object handles. The code is not meant as a tutorial, but you should be able to verify how it works from it.

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

Thanks, i've found an a bug
Not in the library, I hope?

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