Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question dictionary and pointers

Discussion in 'Scripting' started by JimWebs, Sep 8, 2023.

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    71
    As a beginner with unity and c#, hoping someone can give me some "pointers" (pardon the pun)... :) I'm trying to set up a dictionary of "pointers" to a series of functions.
    So I have something like this:
    Dictionary<string,Action> testpointers = new Dictionary<string, Action>()
    {
    {"test1", () => Routine_1() },
    {"test2", () => Routine_2() }
    };

    void Routine_1{}
    void Routine_2{}

    I want to use the values, the pointers, in things like AddListener etc so I can change things dynamically using my dictionary. How do I get the function/method "label" as an "pointer". If I do something like this
    var showme = testpointers["test1"] all I get in debug.log is "System.Action".

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,194
    You can do this but the syntax to "get a pointer" to a function omits the ()

    You don't need to re-wrap it in a delegate, but you're welcome to.

    The () after the function is the "invoke this function"

    To invoke showme, you would do:

    Code (csharp):
    1. showme();
    or if you like living dangerously:

    Code (csharp):
    1. testpointers["test1"]();
    I always advocate pulling it out and testing it for nullness however.
     
    Last edited: Sep 8, 2023
    JimWebs and SisusCo like this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,970
    Code (CSharp):
    1.  
    2. Dictionary<string,System.Action> testpointers =
    3.     new Dictionary<string, System.Action>()
    4. {
    5.     {"test1", Routine_1 },
    6.     {"test2", Routine_2 }
    7. };
    8.  
    9. static void Routine_1() { print("one"); }
    10. static void Routine_2() { print("two"); }
    11.  
    Edit: or if you need to pass an argument:
    Code (CSharp):
    1.  
    2. Dictionary<string,System.Action<object>> testpointers =
    3.     new Dictionary<string, System.Action<object>>()
    4. {
    5.     {"test1", Routine_1 },
    6.     {"test2", Routine_2 }
    7. };
    8. static void Routine_1(object o) { print($"one {o}"); }
    9. static void Routine_2(object o) { print($"two {o}"); }
     
    Last edited: Sep 8, 2023
    Bunny83, JimWebs and Kurt-Dekker like this.
  4. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    71
    Thanks you both for this. Still having a bit of a fix though.
    I'm doing this:
    1. Dictionary<string,System.Action> testpointers =
    2. new Dictionary<string, System.Action>()
    3. {
    4. {"test1", Routine_1 },
    5. {"test2", Routine_2 }
    6. };

      as in the example above.
      Then I try to use the "pointer"/value in the way I would in Python or whatever by simply grabbing it with the key and dumping it in.

      But ....AddListener(testpointers["test1"]) doesn't work. I get "cannot convert from system.action to unityengine.events.unityaction. What I'm trying to do is set up an array of delegates (I think that's what c# calls them, as Kurt in post above points out; I just call them pointers) that I can pick out and use (as in example with AddListener) by using the keys as references.
    I must be missing a step somewhere.
     
    Last edited: Sep 9, 2023
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,194
    Oh yeah, you just gotta wrap it with delegate...

    Syntax would look like:

    Code (csharp):
    1. AddListener( delegate { testpointers["test1"](); } );
    But again, if you think the retrieve might fail, obviously hook up a try/catch or a .ContainsKey() check.
     
    JimWebs likes this.
  6. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    71
    Thank you so much. You're a star. This is finally working and now I can get on. :) Thank you.
     
    Kurt-Dekker likes this.
  7. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    71
    @halley


    Thanks for helping me resolve this as well.