Delegates in AngelScript
#1 Members - Reputation: 921
Posted 17 September 2012 - 11:04 AM
What I'm not sure of, however, is how this should interact with garbage collection (as I understand it, this bites people in the ass with startling frequency in C#, do we need to have a language-level 'weak reference' construct too?) and cross-module function imports. Community, fire away.
#2 Moderators - Reputation: 2310
Posted 17 September 2012 - 04:38 PM
I feel that delegates is currently one of the most important lacking feature in AngelScript, and one that I was planning to work on for the upcoming releases.
I personally don't have any experience to draw from in designing how delegates should work in AngelScript, so this kind of community input will definitely be important to map out how delegates should work.
Personally I want to make the delegates as simple as possible (e.g. no built-in multicasting). I would also like to see the delegates replace the current function pointers, to reduce the amount of redundant features in the library/language.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#3 Members - Reputation: 355
Posted 19 September 2012 - 11:32 AM
It could be a problem if the object that owns the method is destroyed before the delegate, though (I think this is what InvalidPointer is talking about with regards to garbage collection). It probably wouldn't be a good idea to have the delegate keep a strong reference to the object to keep it alive. Maybe there could be a way to invalidate the delegate when its object is destroyed, so calling it would throw an "Unbound function" exception just like calling an empty function handle.
I agree with the decision to keep multicasting out of the engine proper. With the addition of a function call operator it would be reasonably simple to create a custom class that acted as a multicast delegate.
#4 Members - Reputation: 118
Posted 19 September 2012 - 03:40 PM
This delegate system works with events and subscribers, only subscribers has been exposed to my angelscript (ie AS can catch some Engine event but not raise some event).
To expose briefly how it's used:
I register a funcdef in the ASEngine which will be my signature (for instance void MouseMoveEvent(CMouse@, const CVec2f&in))
Then I expose to the script an "event subscriber" (for instance: CMouseMoveEventHandler) that declares a method "SetCallback" which takes in our exemple (IEventHandler@, MouseMoveEvent@). IEventHandler is an empty interface that I declared so AS classes can register and catch events simply by inherit from it (there may be better solutions, and maybe there already is a hidden common parent class to all script classes)
After that all Game Engine that have events expose a RegisterToEventxxx and a UnregisterFromEventxxx (in our exemple RegisterToEventMouseMove(CMouseEventHandler@+) / UnregisterFromEventMouseMove(CMouseEventHandler@+)
Then the class script declares an EventHandler, implements the "callback" method and all is done
(this is pseudo code and may contain syntax errors)
class CScriptCatcher : IEventHandler
{
CScriptCatcher()
{
m_MouseMoved.SetCallback(this, @OnMouseMove);
GetGame().RegisterToEventMouseMove(m_MouseMoved);
}
~CScriptCatcher()
{
GetGame().UnregisterFromEventMouseMove(m_MouseMoved);
}
private void OnMouseMove(CMouse@ _pMouse, const CVec2f& _vDelta)
{
//Do some stuff here to hande mouse move
}
private CMouseMoveEventHandler m_MouseMoved;
}
There certainly a better way and a better interface to use the idea to be a little more lightweight for the user, but it might help you to have an idea on how things could be done.
For the delegate implementation in C++, it's an event class that holds a list of generics subscribers using templates (because the system is primarily used with C++). Subscribers are only a "this" pointer and method pointer OR a function pointer so delegates can be either method members with a this or a static method / a function.
Hope this might help ;)
#5 Moderators - Reputation: 2310
Posted 19 September 2012 - 05:21 PM
I like the C# syntax too (without the multicasting). I think it is the most clean syntax for delegates.
Quittouff:
I'm not sure what customizations you made to the library, but it looks like you don't have a compile time verification of the method signature. Is that right?
There is no common parent class for all script classes, and I do not plan to add one. However, the CScriptHandle add-on would probably work for you as it serves as a generic handle that can hold a reference to any reference object, including script classes.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#6 Members - Reputation: 118
Posted 19 September 2012 - 11:15 PM
And actually yes, this method provide a compile time verification of the callback signature, if you implement a callback with the wrong signature, it will not compile at the "SetCallback" as the second argument is a pointer to a funcdef. It would produce this kind of error: (example taken for my script sorry if names are not very relevant)
Compiling BPE MainMenuForm.bhass (3, 5): INFO: Compiling CMainMenuForm::CMainMenuForm() MainMenuForm.bhass (6, 27): ERROR: No matching signatures to 'CButtonEventHandler::SetCallback(CMainMenuForm&, OnChallengesClick@const)' MainMenuForm.bhass (6, 27): INFO: Candidates are: MainMenuForm.bhass (6, 27): INFO: void CButtonEventHandler::SetCallback(IEventHandler@, ButtonEvent@)(just added an int parameter to the callback method)
So even if the error is not very clear with current implementation, it is triggered when the callback signature is incorrect at compile time.
I didn't think of the CScriptHandle that would have been cleaner indeed. But now the product has been released so it may be for the next one
#7 Moderators - Reputation: 2310
Posted 20 September 2012 - 08:42 AM
Delegates would need something similar to work, as they are supposed to be able to take either a global function or an object method. I think the opCall feature that cellulose is working on will be needed too.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#8 Members - Reputation: 355
Posted 20 September 2012 - 10:12 AM
// This myDelegate = @myObject.prop; // Instead of this myDelegate = @myObject.get_prop;
I have no experience with writing compilers, so I don't know if this is feasible to implement.
#9 Moderators - Reputation: 2310
Posted 20 September 2012 - 10:21 AM
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#10 Members - Reputation: 599
Posted 13 December 2012 - 06:11 PM
A function pointer and an ad-don just as well do the same job.
void GlobalFunction()
{
}
Object obj1;
Object2 obj2;
Signal signal;
signal << function(Object::DoStuff, @obj1);
signal << function(Object2::DoOtherStuff, @obj2);
signal << function(GlobalFunction);
signal.call(); // call all functions, also accepts parameters and refuses to call functions with wrong parameters.
I already do this but with strings and cached method pointers.It just need to be faster with function pointers.
I don't see the point of adding delegates. Or any Observer pattern specific functionality to core language.
#11 Moderators - Reputation: 2310
Posted 13 December 2012 - 08:15 PM
Multicasting and other features of more advanced languages can be added through add-ons if someone should feel they are necessary.
I'll also add that I haven't quite decided if I'll call the feature delegates, or if I'll simply keep using funcdef.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#12 Members - Reputation: 599
Posted 14 December 2012 - 06:33 AM
What do you think about reference type of them?
Strong reference on function pointers if just program flow breaking.
I think AngelScript would highly benefit from a weak reference type.
I can think of a thousand ways to utilize them.
#13 Moderators - Reputation: 2310
Posted 14 December 2012 - 06:42 AM
At least to begin with, the delegate (or function handle) will hold a strong reference to the object. The GC will deal with potential circular references that arises.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#14 Members - Reputation: 599
Posted 14 December 2012 - 07:10 AM
array<funcPtr> events;
class Object
{
Object()
{
events.insertLast(funcPtr(Object::SomeFunc, @this));
}
void SomeFunc() { }
};
void main()
{
Object o;
}
Above code puts to forces script writer to release reference by hand. If she forgets it will be hell to debug why object aren't released.
But a weak reference releases object and shows a null pointer access warning, which is much easier to spot.
That is not to say all function pointers should be weak references, strong references also have their places.
Maybe a new keyword before objects and function pointers to make them weak pointers.
weak Object @o = Object();Probably implementation will be a proxy handle.
I am not experienced with GC languages, probably extending discussion out of my ignorance.
#15 Moderators - Reputation: 2310
Posted 14 December 2012 - 08:37 AM
For the weak reference to become null, so it can give the null pointer exception, it means that the object needs to keep track of all weak references that refer to it, so they can be nulled whenever the object is destroyed. This is not easy to implement in a safe manner, much less with little overhead.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#16 Members - Reputation: 599
Posted 14 December 2012 - 09:32 AM
map<int,void*> handles; // object destructor sets this to null.
class WeakHandle
{
int id;
void *Get()
{
return handles[id].ptr;
}
};
class Function
{
WeakHandle handleToObject;
};
two improvements would be
- use array to lessen cache misses. WeakHandle needs to hold another integer to do that
- weak references hold count so that GC can clean handles array from nulls. Prevent handles array to keep much unnecessary memory.
- promote script objects to weak objects only when they are referenced from weak pointers. so we don't query handles array for every destructor call.
I have used this system to catch nasty memory leaks, it works wonderfully.
Weak references are much easier to implement than strong ones. That is if the existing system permit this kind of flexibility.
I am not trying to push a new feature on you. Just some ideas for the future of this advanced language.
There is nothing like angelscript on the market.
#17 Moderators - Reputation: 2310
Posted 14 December 2012 - 11:17 AM
In C++ you can do it like this because you don't have to bother with the co-existance of weak references and strong references. In AngelScript this would just be a tiny part of the solution required to support weak references. I would also not want to incurr the overhead of keeping track of references to all objects, when most of them are not going to have any weak references anyway.
As I said earlier. If I can think of a proper solution I will definitely add it, as I fully agree that they would provide an important benefit to the library.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#18 Members - Reputation: 184
Posted 27 December 2012 - 10:50 PM
Andreas,
I have a reference counting class that mixes weak and strong references without having to track references to all objects. I use a form of intrusive reference counting so it may not apply directly to your needs.
struct ref_count_struct
{
_int strongCount;
_int weakCount;
};
class reference
{
private:
ref_count_struct* _ref_count;
public:
reference()
{
_ref_count = new ref_count_struct;
_ref_count->strongCount = 0;
_ref_count->weakCount = 0;
}
virtual ~reference()
{
_ref_count->strongCount = 0;
if( _ref_count->weakCount == 0)
{
delete _ref_count;
_ref_count = 0;
}
}
inline void ref()
{
_ref_count->strongCount++;
}
inline void unref()
{
// KEEP IT THIS WAY BUG FIX
if(_ref_count->strongCount == 1)
{
delete this;
}
else
{
_ref_count->strongCount--;
}
}
inline void weakRef()
{
_ref_count->weakCount++;
}
inline void weakUnref()
{
_ref_count->weakCount--;
}
inline _int getRefCount() {return _ref_count->strongCount;}
inline _int getWeakRefCount()
{
return _ref_count->weakCount;
}
inline ref_count_struct* getRefCountStruct(){return _ref_count;}
};
Anyway, the basic idea is to hold the reference counting info in a separate object that sticks around after the object is deleted if and only if the object still has weak references associated with it. Weak_ptrs hold a pointer to the reference counting object so they can check to see if the object has been deleted when access to the object is attempted.
template<class type> class ref_ptr
{
private:
type* _ptr;
public:
ref_ptr()
{
_ptr = 0;
}
ref_ptr(type* ptr)
{
if(ptr != 0)
{
_ptr = ptr;
_ptr->ref();
}
else
{
_ptr = 0;
}
}
ref_ptr(const ref_ptr &ptr)
{
if(ptr._ptr != 0)
{
_ptr = ptr._ptr;
_ptr->ref();
}
else
{
_ptr = 0;
}
}
~ref_ptr()
{
if(_ptr)
{
_ptr->unref();
_ptr = 0;
}
}
inline type* get()
{
return _ptr;
}
inline type* get() const
{
return _ptr;
}
inline type* operator->()
{
return _ptr;
}
inline type* operator->() const
{
return _ptr;
}
inline type& operator*()
{
return *_ptr;
}
inline type& operator*() const
{
return *_ptr;
}
};
emplate <class type>
class weak_ptr
{
private:
type* _ptr;
ref_count_struct* _ref_count;
public:
weak_ptr()
{
_ptr = 0;
_ref_count = 0;
}
weak_ptr(type* ptr)
{
if(ptr != 0)
{
_ptr = ptr;
_ref_count = ptr->getRefCountStruct();
_ptr->weakRef();
}
else
{
_ptr = 0;
_ref_count = 0;
}
}
weak_ptr(const weak_ptr &ptr)
{
if(ptr != 0)
{
_ptr = ptr._ptr;
_ref_count = ptr._ptr->getRefCountStruct();
_ptr->weakRef();
}
else
{
_ptr = 0;
_ref_count = 0;
}
}
~weak_ptr()
{
if(_ref_count != 0)
{
// If the referenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
_ref_count = 0;
_ptr = 0;
}
else
{
delete _ref_count;
_ref_count = 0;
_ptr = 0;
}
}
else
{
_ptr->weakUnref();
_ptr = 0;
_ref_count = 0;
}
}
}
inline type* get()
{
if(_ref_count != 0)
{
// If the refenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
_ref_count = 0;
_ptr = 0;
}
else
{
assert(_ref_count->strongCount == 0 && _ref_count->weakCount <= 1);
delete _ref_count;
_ref_count = 0;
_ptr = 0;
}
}
}
return _ptr;
}
inline type* get() const
{
if(_ref_count != 0)
{
// If the refenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
_ref_count = 0;
_ptr = 0;
}
else
{
assert(_ref_count->strongCount == 0 && _ref_count->weakCount <= 1);
delete _ref_count;
_ref_count = 0;
_ptr = 0;
}
}
}
return _ptr;
}
inline type* operator->()
{
if(_ref_count != 0)
{
// If the refenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
_ref_count = 0;
_ptr = 0;
}
else
{
assert(_ref_count->strongCount == 0 && _ref_count->weakCount <= 1);
delete _ref_count;
_ref_count = 0;
_ptr = 0;
}
}
}
return _ptr;
}
inline type* operator->() const
{
if(_ref_count != 0)
{
// If the refenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
_ref_count = 0;
_ptr = 0;
}
else
{
assert(_ref_count->strongCount == 0 && _ref_count->weakCount <= 1);
delete _ref_count;
_ref_count = 0;
_ptr = 0;
}
}
}
return _ptr;
}
inline type operator*()
{
if(_ref_count != 0)
{
// If the refenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
_ref_count = 0;
_ptr = 0;
}
else
{
assert(_ref_count->strongCount == 0 && _ref_count->weakCount <= 1);
delete _ref_count;
_ref_count = 0;
_ptr = 0;
}
}
}
return *_ptr;
}
inline weak_ptr& operator=(type* ptr)
{
if(_ptr == ptr)
return *this;
// Handle the old observed object
if(_ref_count != 0)
{
// If the refenced object is dead
if(_ref_count->strongCount == 0)
{
// If we aren't the last weak reference
if( _ref_count->weakCount > 1)
{
_ref_count->weakCount--;
}
else
{
assert(_ref_count->strongCount == 0 && _ref_count->weakCount <= 1);
delete _ref_count;
}
}
else
{
_ptr->weakUnref();
}
}
if(ptr != 0)
{
_ptr = ptr;
_ref_count = ptr->getRefCountStruct();
_ptr->weakRef();
}
else
{
_ptr = 0;
_ref_count = 0;
}
return *this;
}
};
I ripped part of my ref_ptr and weak_ptr class implementations out as examples. These are not complete. I don't know if this will help but I would love to see weak references in AngelScript.
- James
Edited by jpmcmu, 27 December 2012 - 11:01 PM.
#19 Moderators - Reputation: 2310
Posted 28 December 2012 - 08:23 AM
Thanks. I actually have something similar in my own game engine.
My game objects are split in two. There is CGameObject that implements all the logic, and there is the CGameObjectLink that provides the weak reference. Each CGameObject has a pointer to the corresponding CGameObjectLink, and whenever the CGameObject needs to be destroyed before all references to it have been removed it simply removes the connection to the the CGameObjectLink. The CGameObjectLink object lives on until all references have been removed, but will no longer forward any calls to CGameObject that has been destroyed.
Making this work generically and as transparently as possible in the script language is not easy though, especially when I do not want to incurr a penalty where weak references are not used.
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
#20 Members - Reputation: 126
Posted 14 January 2013 - 03:12 AM
- Holding a pointer of the method does not increase the reference count of the object which contains it?
- Now function pointers have both of them, weak reference to a object and the pointer of a function?
- How should we implement delegates efficiently?? Would it be difficult to support weak reference in script object class?
Edited by vroad, 14 January 2013 - 03:16 AM.






