Delegate in Dictionary. How to have a function return the actual delegate?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Test : MonoBehaviour {
Dictionary<string, UnityAction> actionByName = new Dictionary<string, UnityAction>();
// Use this for initialization
void Start () {
actionByName.Add("Action 1", null);
actionByName.Add("Action 2", Foo);
actionByName.Add("Action 3", null);
UnityAction action = FindAction("Action 2");
action += Bar;
action();
Debug.Log("-");
actionByName["Action 2"]();
}
// Update is called once per frame
void Update () {
}
UnityAction FindAction (string name) {
return actionByName[name];
}
void Foo () {
Debug.Log("Foo");
}
void Bar () {
Debug.Log("Bar");
}
}
As you can see, line 19 and line 21 produce different result. I want to be able to modify the return of FindAction (), which effects the actual delegate in actionByName. Is there a way to do that?
I'll comment ins$$anonymous$$d of answer my own question because I'm new to certain topics of C# and afraid to be misleading. Still I found some helpful things so in case anybody has same problems.
Blah
Basically what I was trying to achieve is this: How to create a dynamic "broadcast" system using Events. But Event and Delegate are new to me. After some research I decided that I started from the beginning: use only delegate. But what I needed is more than the tutorial. I needed to find certain delegate in a Dictionary and += -= to it. Hence the question.
Helpful links
What is the difference between a reference type and value type in c#?
C# ref is it like a pointer in C/C++ or a reference in C++?
How is UnityAction different from .NET's built-in Action?
Pass delegate to coroutine as a variable
Final
In the last listed article, the answer mentioned a way of using wrapper class. So here it is. I get Foo Bar - Foo Bar in Console now.
// http://answers.unity3d.com/questions/1345562/pass-delegate-to-coroutine-as-a-variable.html
// https://forum.unity.com/threads/how-is-unityaction-different-from-nets-built-in-action.288873/
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Test : $$anonymous$$onoBehaviour {
Dictionary<string, ActionWrapper> actionByName = new Dictionary<string, ActionWrapper>();
// Use this for initialization
void Start () {
actionByName.Add("Action 1", null);
actionByName.Add("Action 2", new ActionWrapper(Foo));
actionByName.Add("Action 3", null);
ActionWrapper action = FindAction("Action 2");
action.Add(Bar);
action.Invoke();
Debug.Log("-");
actionByName["Action 2"].Invoke();
}
// Update is called once per frame
void Update () {
}
ActionWrapper FindAction (string name) {
return actionByName[name];
}
void Foo () {
Debug.Log("Foo");
}
void Bar () {
Debug.Log("Bar");
}
public class ActionWrapper {
public Action action;
public ActionWrapper (Action newAction) {
action = newAction;
}
public void Add (Action newAction) {
action += newAction;
}
public void Remove (Action newAction) {
action += newAction;
}
public void Invoke () {
action();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612141636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I detect what animation condition an animator is in through scripting? 0 Answers
Silly compiler error is telling me something isnt a variable when it surely is. 3 Answers
Same script activating separately on different game objects? 0 Answers
rpc message does not work 0 Answers
How do I rotate the GameObejcts smoothly with negative degrees 0 Answers