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?