Hi and thanks for reading.
I've been looking at how to implement callback functions that I can pass to the application from script, and subsequently call from within the application.
A while back, when funcdefs didn't exist yet, I implemented my own Delegate class to solve this problem, but the problem with those is, that I cannot call object methods/actual delegates with those, since I cannot find a way to pass the script-class object reference to the application.
The documentation shows how to create a script class from within the application, and then call a function on that, but I have no idea how to pass a script object that was created in script to the application for calling.
Now I've updated to version 2.27.1 of Angelscript, and I found out it supports function definitions and delegates. I think this is a great solution to the problem. My delegate class for instance, couldn't match arguments, so it wasn't really safe. But with funcdefs I have an ownership problem.
When I create a delegate from an object method, the delegate object refs the object. So the object cannot be cleaned up while this delegate exists. Now if I proceed to pass this delegate to the application, I get an asIScriptFunction object. If I want to store that asIScriptFunction for later use, I reckon I'll have to increase it's ref.
Now the problem is, that the scripter is now no longer in control over when his object is destroyed, or when the delegate is released. If for instance, he registers an object method in a delegate as an event handler for, let's say, a key press, I'd have to return a certain handle, that he can later use to explicitly deregister this even, and thus "release" the asIScriptFunction object, in turn releasing the delegate, in turn releasing his object. This feals really redunant, since the delegate already represents this bond.
However if I don't ref the asIScriptFunction pointer in the application, I have no way of knowing when the delegate is released in script. Some kind of Weak referencing or a callback could work.....
I'm not sure which direction I want to take, or what is a good approach.
Is there a way to pass a script class object to the application so I can implement my own delegate?
Or should I stick to returning a new handle object that the scripter needs to keep reffed for as long as the delegate should stay alive?
In the last case generic func defs would be nice. Andreas talked about these when funcdefs were introduced, but I don't think they exist yet?
Any tips are appreciated. Many thanks,