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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Is there a way to convert a string to an event/delegate so I can dynamically subscribe methods

Discussion in 'Scripting' started by Xerun1, Nov 10, 2022.

  1. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    I have a few buttons that each trigger a different attack for the playing character. On pressing the button the attack is stored into a string and then the player chooses a direction and hits confirm to fire the attack off.

    Currently I"m using Invoke(string of attack) to achieve the desired result.

    Now I want to alter this so that instead of immediately invoking the attack I subscribe the string to an event manager that does the attack once certain conditions are met. I have the event listener working fine but I can't seem to find the correct syntax so that I can use:

    EventManager.Actions += string of attack

    is this possible?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Why can't you just subscribe the method to perform itself, rather than using fragile strings?
     
    Bunny83 likes this.
  3. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    Because I want to be able to pass multiple methods based off which button I press. I can't just pass the Attack Method in the button, because I need it to start the Set Direction Method and then accept player direction and then confirmation.

    I figured the best method would be to subscribe since that method takes a string basically anyway. So then it can dynamically pick the method based on what I put in
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Then you want to wrap up these commands in objects themselves (plain classes), like a rudimentary command pattern. Thus you can issue these objects as 'groups of commands' to an overarching system that operates over each of them.

    I can't condone the usage of strings because there's always a better way.
     
    Bunny83 likes this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,384
    Can you show us what code you currently have?

    If you show us that, we might be able to show you how to pull this off using a command pattern, likely using delegates.

    ...

    To your original question you can't just use a string as a method delegate (which is what your syntax suggests). Technically yes you could reflect out the method, create a delegate, and so on. But that's a lot of silliness for what could be simpler.

    Alternatively you could use an anonymous delegate to call Invoke. But if you need to unregister that, it'll prove difficult.

    Otherwise you could again, create a command pattern, but use strings. But if you're going all that way... might as well do it correctly.
     
    spiney199 likes this.
  6. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15

    So drilling down to just the specifics because the whole code is fairly long :

    Button -> Initialize Attack Method (str EventualMethodName)

    InitializeAttack is a set direction method this changes a variable state to Direction

    then in Update the player selects a direction using the arrow keys, each key sets an int value called Attack Direction
    and also sets the state to Confirm

    then in update the player hits confirm button, this resets state to null, sets Attack Direction to an impossible int value

    Previously at this point I was using Invoke and calling the previously set string to run the method. this determines the attacks values and each method does something different. this was working perfectly.


    now I want to have the player move and when they reach their desired position then execute the attack. Subscribing to an event seemed the best method and then calling that event when they reach the position
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Then as suggested you should look at the command pattern, which is a way to record actions and run them at a later point in a specific order.

    Also worthwhile learning about delegates while you're at it.
     
    Xerun1 likes this.
  8. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    This looks perfect! Thank you very much. I'm very much self taught so I only know things I've encountered before.