Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help with collections, triggers, dialogue

Discussion in 'Scripting' started by myyokeisheavvy, Apr 2, 2020.

  1. myyokeisheavvy

    myyokeisheavvy

    Joined:
    Apr 18, 2018
    Posts:
    3
    Hi, since answers is down I'm having trouble finding an answer to my problem, even what to look up. I'm really new to coding so I hope this isn't a super dumb question but I have a small game with a lot of dialogue triggers, built based off of this video:

    I've said it up so that each item has a trigger surrounding it and whenever the player enters that trigger it sets active a button for them to click to trigger the dialogue specific to that item.
    My problem is that there's a "collection" of four of the same items throughout the very small map. I'd like the player to find the first one, get a set of dialogue that says "there's four of these throughout", then be able to find the rest, then on the last one say "that's it! you find them!"
    I could get around this if there was a set order, but the map is relatively small and the player needs to be able to find them in any order, so I'm not sure how to go about solving that, or even what that concept is in programming (arrays?)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The general process is to produce a list of these things, which you then populate using the Unity Editor.

    Whenever one is triggered, it marks a variable within it that says "I'm triggered."

    Within the script that has the full list, there would be another function that checks "Hey, has everything in this list been triggered?" and if so, go do something else, like change scenes or pop up another dialog, etc.
     
  3. kasztelan

    kasztelan

    Joined:
    Nov 3, 2014
    Posts:
    11
    So if I understand correctly you want each item to only trigger once and depending how many items you've collected so far you want to display different message

    In that case I'd modify the DialogueTrigger class and include information if it particular trigger was already invoked.

    Now for the second problem, you have to store somewhere the information how many items you've collected so far which in case translates to how many times the trigger was invoked. There are many ways to do that, don't know your code so can't say what's the best option for you. Lets keep it simple and store that information inside Dialogue class (I'm assuming all 4 items use the same instance of Dialogue class).

    Then you'd have to modify the DialogueManager to accept optional arguments for starting conversation - at which point in messages array it should start and how many messages should it show.

    Here is the modified code, I haven't tested it by any means so can't guarantee it'll work for you

    Code (CSharp):
    1. public class DialogueTrigger : MonoBehaviour
    2. {
    3.     public Dialogue dialogue;
    4.     public bool Triggered;
    5.     public void TriggerDialogue()
    6.     {
    7.         if (!Triggered)
    8.         {
    9.             Triggered = true;
    10.             FindObjectOfType<DialogueManager>().StartDialogue(dialogue, dialogue.TimesSeen, 1);
    11.             dialogue.TimesSeen++;
    12.         }
    13.     }
    14. }
    Code (CSharp):
    1. public class Dialogue
    2. {
    3.  
    4.     public string name;
    5.     public int TimesSeen = 0;
    6.  
    7.     [TextArea(3, 10)]
    8.     public string[] sentences;
    9.  
    10. }
    And StartDialogue method

    Code (CSharp):
    1. public void StartDialogue(Dialogue dialogue, int start = 0, int messagesToShow = 0)
    2.     {
    3.         animator.SetBool("IsOpen", true);
    4.  
    5.         nameText.text = dialogue.name;
    6.  
    7.         sentences.Clear();
    8.  
    9.         int endIndex = messagesToShow == 0 ? dialogue.sentences.Length : (start + messagesToShow);
    10.         for (int i = start; i < endIndex; i++)
    11.         {
    12.             sentences.Enqueue(dialogue.sentences[i]);
    13.         }
    14.  
    15.         DisplayNextSentence();
    16.     }