Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Delegates in AngelScript


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
26 replies to this topic

#1 InvalidPointer   Members   -  Reputation: 1445

Like
1Likes
Like

Posted 17 September 2012 - 11:04 AM

Currently thinking about how delegates should work, as I find myself becoming really, really hamstrung without their inclusion in AngelScript. While I think I have some of the implementation worked out, I'm curious what people would want in an implementation and what the syntax should be. From a technical perspective, I think it's best approached at a single-subscriber level, with a delegate storing a function to call and an additional 'this' pointer; multicast delegates/events could be either a library add-on extending the built-in array type or something left to the application interface to provide.

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.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 17 September 2012 - 04:38 PM

Ah, you beat me to this subject :)

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#3 Jake Albano   Members   -  Reputation: 649

Like
0Likes
Like

Posted 19 September 2012 - 11:32 AM

I think the C# way is the best approach as far as language features go. Being able to declare a funcdef / delegate type in-place would be a huge help, as would the ability to take a handle to a member function by storing a "this" pointer if necessary.

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 Quittouff   Members   -  Reputation: 118

Like
0Likes
Like

Posted 19 September 2012 - 03:40 PM

I made some delegates system in C++ that I exposed to Angel script in my project with minor modifications of a v2.22.0 WIP of AS (in order to be able to specify a method adress).

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 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 19 September 2012 - 05:21 PM

Jake Albano:

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#6 Quittouff   Members   -  Reputation: 118

Like
0Likes
Like

Posted 19 September 2012 - 11:15 PM

The only modifications I made is being able to point a method address: I added a "IsSignatureExceptNameAndObjectEqual" that is the same as "IsSignatureExceptNameEqual" and does not care about the object type, so a funcdef is not dependent on the object type but only on the method signature. It may not be ideal but it was the fastest way for me to do what I wanted.

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 :P.

#7 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 20 September 2012 - 08:42 AM

It's a good solution for what you wanted.

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#8 Jake Albano   Members   -  Reputation: 649

Like
0Likes
Like

Posted 20 September 2012 - 10:12 AM

If I may request a feature to be included in this: When taking the handle of an object method, it would be awesome to be able to get accessor functions by their short names. For example:

// 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 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
1Likes
Like

Posted 20 September 2012 - 10:21 AM

It's tricky, but can be done similarly to how the property accessors are evaluated already, i.e. the decision to use either set or get needs to be deferred until the very last moment when it can be determined which function signature the delegate is expecting.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#10 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 13 December 2012 - 06:11 PM

Is Delegate a confirmed feature for AngelScript?

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 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 13 December 2012 - 08:15 PM

Delegates are confirmed. However, I have no plans on making them as powerful as in C# with multicasting and the likes. AngelScript's delegates will be pretty much the existing funcdef & function handle feature, with the additional support to take a handle to a class method with its corresponding object pointer. Similar to the example you posted.

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#12 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 14 December 2012 - 06:33 AM

That puts me to rest. Simple is the best.

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 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 14 December 2012 - 06:42 AM

It's difficult to make weak references work safely without putting the responsibility in the hands of the script writer. Unless I can figure out a proper solution, weak references are not going to happen.

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#14 saejox   Members   -  Reputation: 714

Like
1Likes
Like

Posted 14 December 2012 - 07:10 AM

I am sorry :(, i don't understand.

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 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 14 December 2012 - 08:37 AM

You're absolutely correct on the benefits on weak references. However, you're only thinking on how they would be used, not how they have to be implemented in the AngelScript engine :)

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#16 saejox   Members   -  Reputation: 714

Like
1Likes
Like

Posted 14 December 2012 - 09:32 AM

There is also this very simple implementation:

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 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
1Likes
Like

Posted 14 December 2012 - 11:17 AM

I appreciate the suggestions and I'll keep them for later, but it is not quite as simple as you think.

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.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#18 jpmcmu   Members   -  Reputation: 184

Like
0Likes
Like

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.

Sleep is for the weak, or at least that is what I tell myself every morning.

#19 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

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. 


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

#20 vroad   Members   -  Reputation: 131

Like
0Likes
Like

Posted 14 January 2013 - 03:12 AM

I want delegates too, but this is complicated problem. It's maybe too difficult to understand for me.
  1. Holding a pointer of the method does not increase the reference count of the object which contains it?
  2. Now function pointers have both of them, weak reference to a object and the pointer of a function?
  3. 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.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS