NPC Dialog
Hi, I try to make my own NPC dialog script. I figured out that way:
1.Lock players movement (so he cant run with chat opened)
2.Display first msg from NPC into text in panel + 3 choose options.
(1 yes, 2 no, 3 cut dialog)
3.Code waits till player press one button. <-- ACTUAL PROBLEM #1 --
Every button have own value. Btn3 = 3, Btn2 = 2, etc.
4.Code have one int variable = 0 and after button pressed change that value by button's value.
5.After that code switch(variable)
case 1: case 2: case 3: <-- third always finish dialog
Every case have NPC's answer.
6.After switch int variable = 0;
7.Again wait for players press button, and again variable change value.
Repeat that till dialog finish.
When dialog ends:
Put dialog panel out of canvas(invisible for player), unlock players movement.
Does it have sense?
IMO #1 problem is force code to wait till player choose button.
Answer by BTCallahan · May 06, 2016 at 07:51 PM
To keep the player from moving around, you could have a boolean called "isTalking". If it's active, the player's input is disabled. For example:
public bool isTalking;
void FixedUpdate(){
if(isTalking){
//do something
}else{
//get controller input
}
}
For the buttons, I recommend having a child of the canvas, say a panel, that starts out deactivated (and thus hidden) which would have several buttons that could be hidden or unhidden using a script depending on the number os responses you needed to display.
Regarding the dialogue, I think using a custom class to store it would suit you best. Take the following:
public class DialogueStorage{
public string npcText;
public string[] playerTexts;
public int[] responceNumberToGoTo;
public DialogueStorage(string npcText, string[] playerTexts, int[] responceNumberToGoTo){
this.npcText = npcText;
this.playerTexts = playerTexts;
this. responceNumberToGoTo = responceNumberToGoTo;
}
}
Each NPC could have an array of the DialogueStorage objects.
public class NPC : MonoBehaviour{
public DialogueStorage[] NPC_Dialogue;
public string GetNPC_Responce(int responceNumber){
return NPC_Dialogue[responceNumber].npcText;
}
public string[] GetPlayerResponce(int responceNumber){
return NPC_Dialogue[responceNumber].playerTexts;
}
public int[] GetResponceNumbers(int responceNumber){
return NPC_Dialogue[responceNumber].responceNumberToGoTo;
}
}
The script could then check the length string array returned by the GetPlayerResponce method to determine the number of button that should be visible and the array itself to set the button texts.
I hope this helps you out.