0

I'm working on a game (in Unity) that has dialog, and sometimes the player will need to select an option based on the dialog or the type of person they are talking to. Based on what the player chooses the game needs to call a method, but depending on the scenario the callback method could have a parameter, other times it might not have any parameters. For example if they are being given a quest they would see the options "Accept" or "Refuse". Accept would need to call AcceptQuest(Quest quest), and refuse would need to call RefuseQuest().

Right now I have it pass an array of Action delegates to the start dialog method, that sets the global Action array, then and then the button the player clicks on calls the associated callback.

So the player would initiate the conversation with the shopkeeper:

public void Interact(Action[] actDialogOptions)
{
    DialogManager.instance.ShowDialog(npcDialog, actDialogOptions);
}   

That would start the dialog:

Action[] actDialogOptions; //Allows the button to grab the correct delegate

public void StartDialog(string[] dialogLines, Action[] actPassedDialogOptions)
{        
    //Code to Display the text
   
    actDialogOptions = actPassedDialogOptions
}

//Method mapped to one of the buttons
public void DialogOption1Clicked()
{
    actDialogOptions[0]?.Invoke();
}

//Method mapped to one of the buttons
public void DialogOption2Clicked()
{
    actDialogOptions[1]?.Invoke();
}

I want to call the Interact method like this, but obviously I cannot:

public void TalkToNPC()
{
    actDialogOptions = new Action[] { AcceptQuest, RefuseQuest };
    Interact(actDialogOptions);
}

public void AcceptQuest(Quest quest)
{
    //Code to accept
}

public void RefuseQuest()
{
    //Code to Refuse
}

So how would I go about doing this?

Vandel212
  • 1,074
  • 1
  • 13
  • 28
  • Clicking a button seems like an `Action`. But that action could be `() => Accept(quest)`. – Jeremy Lakeman Jan 20 '21 at 06:32
  • What quest would the player accept if they chose the "Accept" dialog option? – Sweeper Jan 20 '21 at 06:32
  • Are you looking for params keyword (for multiple arguments of same type) and default value of arguments (set as null)? – Prateek Shrivastava Jan 20 '21 at 06:35
  • @JeremyLakeman Well this is meant to handle multiple situations, another example would be a shopkeeper, so "buy" or "sell". They have nothing to do with quests, so I'm trying to keep it as loosely coupled as possible. – Vandel212 Jan 20 '21 at 06:35
  • @Sweeper That depends on the NPC they talk to. Different NPCs would initate different quests. – Vandel212 Jan 20 '21 at 06:36
  • I understand, but how does each NPC know which quest they are supposed to give out? Does each NPC have a variable of type `Quest` that stores the quest they have? – Sweeper Jan 20 '21 at 06:38
  • Your `Action[]` obviously must contain only `Action` instances, which precludes parameters. But you can wrap calls that do use parameters in `Action` delegates. See duplicate. Of course, you will need to be able to express your wrapped method call as something that can retrieve the `Quest` object; your question provides zero insight as to where that object comes from, so it's not possible to say exactly what that'd look like. There are lots of other, more complicated ways to approach this too, but a question seeking those sorts of answers would "Need Focus". – Peter Duniho Jan 20 '21 at 06:53
  • The button calls an Action. But the action is a parameter that was passed into the dialog. That action can be a delegate which has captured a local variable to pass to another method. – Jeremy Lakeman Jan 20 '21 at 23:51

0 Answers0