Search Unity

UnityEvent returning some feedback

Discussion in 'Scripting' started by Doylgaafs, Aug 19, 2017.

  1. Doylgaafs

    Doylgaafs

    Joined:
    Mar 9, 2016
    Posts:
    6
    Hello!
    I'm making a simple dialogue system for my game. I've got a class Decision that derives from Scriptable Object so I can make my dialogues easily in editor. I want to add a function bool isPossible() which would check if a list of functions attached in inspector are all returning true. Example:

    We are talking with character "Pretty Girl". All the options are:
    - Bye! (always possible)
    - That's a flower for you! (only possible if I have a flower in my inventory)
    - Did it hurt when you fell from heaven? (only possible if your charisma is over 9000)
    - Would you like to see a magic trick? (only possible if your wisdom is over 8 and you have flower in your inventory).

    I've got object "functions" with all important functions (bool searchForItem(Item), bool checkAtributeLevel(Atribute), bool checkIfQuestCompleted(Quest)).


    I think it's clear now. My first idea was to use UnityEvent to simply attach all the functions in inspector easily. But the problem is that I can't get any information about values returned by those functions (actually I can't even attach functions returnig anything). The second idea I had was to add a second parameter which would be the decision which invoked the function so I can do some operations which would help me get the right feedback. But unfortunately UnityEvent does not support multiple parameters (I found a metod for parameters of type declared in advance which is a problem as I sometimes need to pass an item and other time I need to pass an atribute or anything.
    The last idea I had was to make a list of all items needed, all atributes levels needed, quests completed and any possible condition. But that's a lot of lists and it won't be easy to add new conditions later.
    I want to make a flexible system which will work in many games not just in this one I'm currently working on.
    Is there any way I can make it work?
    Thank you Unity Community for all the answers in advance! :)
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Use a Momento Object. Define an class which serves as a Container for all the data the invoker sends. Anything a listener needs will be found inside that container. the listener can then perform a momento.IsPossible &= [true/false]; after all the listeners run they all would have touched that IsPossible without knowing about the other listeners.

    The invoker, which still has a reference to the momento that it sent out, can then check that "IsPossible". Thus it received data back from all the listeners without it actually knowing who the listeners are.

    Better yet, you can also make the Memento adopt the Mediator Pattern (i.e. like how GetComponent works) so that the various listeners can check if the memento has the data it wants (i.e. bool GetValue<T>(string valueName, ref T value), returns true if the memento has the property and sets the value passed in by reference, false otherwise). And if the memento doesn't have the data then the listener can choose to do nothing, or calls something like momento.SetValue<bool>("IsPossible", false).