Search Unity

Few newbie questions

Discussion in 'Scripting' started by urumizawa, Feb 13, 2021.

  1. urumizawa

    urumizawa

    Joined:
    Dec 30, 2020
    Posts:
    32
    No code to show here, just general questions:

    • I made a simple text parser using ink's integration plugin, nothing too fancy but working better than everything I tried, I want to expand on it a little bit by detecting custom functions(?), E.g: if there is [Player] replace it with the name of the player, if the text is
      [Speaker]John:[/Speaker] I am john
      then remove john from that string and set it as a separate string for the speaker.... what would be the best approach to achieve that? or what are the functions i should be looking at?
    • Follow up to the previous question, what would be the best approach to handle all those functions. dictionaries?
    • Speaking of dialogue, what would be the best approach to handling variables for dialogue, in other words, to keep track of what dialogues played and what didn't , what conditions are true and what conditions are not? the best thing i can think of would be 2 dictionaries, one with the condition variables one for the visited dialogues, make a serializable array of conditions and check them before playing the dialogue. I guess whatever the answer to that is would also apply to quests later on.

    Thanks a lot in advance.
     
  2. urumizawa

    urumizawa

    Joined:
    Dec 30, 2020
    Posts:
    32
    posting this as a reply because it is longer and less important but I feel like my code could be cleaner than the mess i have right now, wanted to make the parser script reusable for all dialogue and have a reader script have all the parameters, but it ended up the other way around

    Code (CSharp):
    1. public class InkCustomParser : MonoBehaviour
    2.     {
    3.         // Compiled json asset
    4.         Story _inkStory;
    5.         bool firstExecution = true;
    6.  
    7.         [SerializeField] TextMeshProUGUI dialogueTextBox;
    8.         [SerializeField] Button[] options;
    9.        
    10.  
    11.  
    12.         public void ReadStory(TextAsset textToRead)
    13.         {
    14.             if (firstExecution)
    15.             {
    16.                 SetStoryToRead(textToRead);
    17.                 ContinueStory();
    18.  
    19.  
    20.                 firstExecution = false;
    21.             }
    22.  
    23.             if (_inkStory.currentChoices.Count > 0)
    24.             {
    25.                 DisplayButtons();
    26.             }
    27.             if (_inkStory.canContinue && Input.GetKeyUp(KeyCode.Return))
    28.             {
    29.                 ContinueStory();
    30.             }
    31.            
    32.  
    33.             if (!_inkStory.canContinue && _inkStory.currentChoices.Count == 0 && Input.GetKeyUp(KeyCode.Return))
    34.             {
    35.                 EndOfStory();
    36.             }
    37.         }
    38.  
    39.        
    40.  
    41.         private void DisplayButtons()
    42.         {
    43.             for (int choiceIndex = 0; choiceIndex < _inkStory.currentChoices.Count; ++choiceIndex)
    44.             {
    45.                 Choice choice = _inkStory.currentChoices[choiceIndex];
    46.                 options[choiceIndex].gameObject.SetActive(true);
    47.                 options[choiceIndex].GetComponentInChildren<TextMeshProUGUI>().text = choice.text;
    48.             }
    49.         }
    50.  
    51.         private void HideButtons()
    52.         {
    53.             for (int buttonIndex = 0; buttonIndex < options.Length; ++buttonIndex)
    54.             {
    55.                 options[buttonIndex].gameObject.SetActive(false);
    56.             }
    57.         }
    58.  
    59.         private void ContinueStory()
    60.         {
    61.             dialogueTextBox.text = _inkStory.Continue();
    62.         }
    63.  
    64.  
    65.  
    66.  
    67.         private void SetStoryToRead(TextAsset textToRead)
    68.         {
    69.             _inkStory = new Story(textToRead.text);
    70.         }
    71.        
    72.        public void ChoosePath(int index)
    73.         {
    74.             Debug.Log("index is " + index);
    75.             _inkStory.ChooseChoiceIndex(index);
    76.  
    77.             HideButtons();
    78.             ContinueStory();
    79.         }
    80.  
    81.  
    82.  
    83.         private void EndOfStory()
    84.         {
    85.             HideCanvas();
    86.             _inkStory = null;
    87.         }
    88.  
    89.         private void HideCanvas()
    90.         {
    91.             gameObject.GetComponentInChildren<Canvas>().gameObject.SetActive(false);
    92.         }
    93.     }
    94.  
    95. }

    below is the reader that was supposd to contain the parameters

    Code (CSharp):
    1.  public class InkCustomReader : MonoBehaviour
    2.     {
    3.         [SerializeField] TextAsset dialogue;
    4.  
    5.         InkCustomParser dialogueParser;
    6.         private bool dialoguePlayed = false;
    7.  
    8.         private void Awake()
    9.         {
    10.             if (dialogue == null)
    11.             {
    12.                 Debug.Log("No Dialogue To read, script stopped from executing");
    13.                 this.enabled = false;
    14.             }
    15.  
    16.         }
    17.  
    18.         private void Start()
    19.         {
    20.             dialogueParser = FindObjectOfType<InkCustomParser>();
    21.         }
    22.  
    23.         private void Update()
    24.         {
    25.             if (!dialoguePlayed)
    26.             {
    27.                 StartDialogue(dialogue);
    28.                
    29.             }
    30.            
    31.         }
    32.  
    33.         private void StartDialogue(TextAsset dialogue)
    34.         {
    35.             dialogueParser.ReadStory(dialogue);
    36.  
    37.         }
    38.     }