I'm exposing a C++ Class to AS (WebsocketClient), and it should to be destroyed as the class instantiating it is destroyed.
When the C++ Class is destroyed, it's destructor runs Disconnect() method.
So, for the callbacks functions I'm breaking up as the Delegates example on https://www.angelcode.com/angelscript/sdk/docs/manual/doc_callbacks.html.
The odd behavior:
After some fix suggested on other discussion, It almost works as expected, I could pass a delegate for callback, and when destroyed, WebsocketClient destructor and Disconnect() are called, unless the method used for callback is defined in the same class as the one that instantiate a WebsocketClient object.
Seemed to be some kind of circular references, but then I added another object in the circle and it worked.
Some code:
funcdef void CALLBACK();
void PrintConnectedB()
{
print("AS >>> is connected! ");
}
class teste
{
void PrintConnected()
{
print("AS >>> is connected! ");
}
}
class GameScene : Scene
{
...
private WebsocketClient@ m_room;
teste a;
void PrintConnected()
{
print("AS >>> is connected! ");
}
void onCreated()
{
...
@m_room = WebsocketClient();
// new instance
// test one: passing global function
m_room.SetOnConnect(@PrintConnectedB);
// Works as expected, connection is made, and when destructing GameScene, WebsocketClient destructor is called
if(!m_room.IsConnected())
// connect
m_room.Connect("localhost", "1201");
...
}
...
}
...
// Passing a method from a class instantiated on this
m_room.SetOnConnect(CALLBACK(a.PrintConnected)); // Works as expected, connection is made, and when destructing GameScene, WebsocketClient destructor is called
...
...
// Passing method from this object.
m_room.SetOnConnect(CALLBACK(this.PrintConnected)); // Connection is made, but when destructing GameScene, WebsocketClient destructor is not called
...
I also tried instantiating teste the same way as WebsocketClient with:
private teste@ a;
// then
@a = teste();
That worked as well.