Delegates with parameters

So I’m at that point where I need to work with delegates. Let’s face it, their pretty useful, but not easy to use at all, not even when you just beginning with them.

I’ve documented my problem in comments in the code, otherwise feel free to ask questions.

using UnityEngine;
using System.Collections;
using System;

public class CodeDebugTesting : MonoBehaviour {

	//Delegate variable with type of void
	public delegate void CommentMethod(string text);

	//Runs once at the start of the program
	void Start() {

		//Removing the parameter of 'string' in the delegate and the 'string' of Say method
		//and then trying to use the method BELOW, it works. But otherwise WITH parameters it won't.
		WriteText(Say); // This would work, if the Delegate and Say didn't had a parameter.

		//Doesn't work..
		WriteText(Say("First comment"));
		WriteText(Say("Second comment"));
	}

	//Function to simple Debug Log a comment
	public void Say(string comment) {
		Debug.Log(comment);
	}

	//Gets an input of a delegate of type void, then uses it between two Debug Logs
	public void WriteText(CommentMethod method) {

		Debug.Log("START");
		method();
		Debug.Log("END");

	}

}

using UnityEngine;
using System.Collections;
using System;

public class CodeDebugTest : MonoBehaviour {
	
	//Delegate variable with type of void
	public delegate void CommentMethod(string text);
	
	//Runs once at the start of the program
	void Start() {

		CommentMethod cm = Say;

		//Removing the parameter of 'string' in the delegate and the 'string' of Say method
		//and then trying to use the method BELOW, it works. But otherwise WITH parameters it won't.
		WriteText(cm, string.Empty); // This would work, if the Delegate and Say didn't had a parameter.
		
		//Doesn't work..
		WriteText(cm, "First Comment");
		WriteText(cm, "Second comment");
	}
	
	//Function to simple Debug Log a comment
	public void Say(string comment) {
		Debug.Log(comment);
	}
	
	//Gets an input of a delegate of type void, then uses it between two Debug Logs
	public void WriteText(CommentMethod method, string comment) {
		Debug.Log("START");
		method(comment);
		Debug.Log("END");
	}
}

Hi, here’s my suggestion. It’s a different way using System.Action:

using UnityEngine;
using System.Collections;
using System;

public class CodeDebugTest  : MonoBehaviour 
{
	//Runs once at the start of the program
	void Start() 
	{
		//Send parameter Say (a function) as the action
		WriteText(Say, string.Empty);
		//Note that, the Say method doesn't return anything is 'void-type'. So the WriteText function what receives is the reference
		//to what method to call and then call it sending its respective parameters
		WriteText(Say, "First Comment");
		WriteText(Say, "Second comment");
	}
	
	//Function to simple Debug Log a comment
	public void Say(string comment)
	{
		Debug.Log(comment);
	}
	
	//Using actions, so you don't have to make a reference var to the method, it's more generic 
	public void WriteText(System.Action<string> method, string comment)
	{
		Debug.Log("START");
		method(comment);
		Debug.Log("END");
	}
}

Hey there,

  • If you want to learn everything about delegates (Actions, Funcs, Predicates, Anonymous methods, lamda expression, etc) and how they work,
    Jamie King is the best source.
  • See my answer here on how to
    make delegates survive an assembly
    reload.
  • For a full solution on serializable/inspectable delegate, see my
    uFAction.