Search Unity

Quest log UI system help

Discussion in 'Scripting' started by Calmax, Jan 20, 2020.

  1. Calmax

    Calmax

    Joined:
    Aug 8, 2017
    Posts:
    26
    Hi all,

    I am in the process of creating a quest system. I have got quests such as kill or collect all registering and marking as complete.

    However, I am struggling with putting the quests themselves into a quest window UI. All I want it to do is display the current active quests to the player (quest name, description and reward). I've been trying to figure this out for a while and thought anyone here could give some advice.

    For reference, here is the code for the Quest class and the quest log UI:

    Code (CSharp):
    1. public class Quest: MonoBehaviour
    2. {
    3.     public List<Quest> Quests { get; set; } = new List<Quest>();
    4.     public List<Goal> Goals { get; set; } = new List<Goal>();
    5.     public string QuestName { get; set; }
    6.     public string Description { get; set; }
    7.     public int ExperienceReward { get; set; }
    8.     public bool Completed { get; set; }
    9.     public Item ItemReward { get; set; }
    10.  
    11.     public void CheckGoals()
    12.     {
    13.         Completed = Goals.All(g => g.Completed);
    14.     }
    15.  
    16.     public void GiveReward()
    17.     {
    18.         if (ItemReward != null)
    19.             InventoryController.Instance.GiveItem(ItemReward);
    20.         Debug.Log("Player given reward!");
    21.     }
    22. }
    Code (CSharp):
    1. public class QuestLogUI : MonoBehaviour
    2. {
    3.     bool isLogOpen = false;
    4.     public GameObject Panel;
    5.  
    6.     public Text questName;
    7.     public Text questDescription;
    8.  
    9.     public Quest activeQuest;
    10.  
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.Q))
    15.         {
    16.             isLogOpen = !isLogOpen;
    17.             ShowQuestPanel(isLogOpen);
    18.         }
    19.     }
    20.  
    21.     void ShowQuestPanel(bool b)
    22.     {
    23.         Panel.SetActive(b); //opens/closes log
    24.  
    25.         questName.text = activeQuest.QuestName;  //assign a Quest
    26.         questDescription.text = activeQuest.Description;
    27.     }
    28. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What issue are you running into? It looks correct to me. As long as activeQuest has a value.
     
  3. Calmax

    Calmax

    Joined:
    Aug 8, 2017
    Posts:
    26
    Hi,
    The issue I'm having is giving activeQuest a value. I feel it should be so simple?

    My quests are assigned in the following function in the QuestGiver Class:

    Code (CSharp):
    1.  public void AssignQuest()
    2.     {
    3.         AssignedQuest = true;
    4.         Quest = (Quest)quests.AddComponent(System.Type.GetType(questType));
    5.         Quest.Quests.Add(Quest);
    6.  
    7.         //AddToQuestLog(); ??
    8.     }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    The problem I'm seeing is you have a Quest class which contains a list of Quest... You should consider a QuestManager class instead.

    Now, that being said I wouldn't normally do things this way. I would have a QuestManager class and a Quest data class that I could deserialize a json string into. While I suggest you consider a different structure, the only thing I can tell you now is how do you determine what is an active quest?

    I mean, in normal gameplay. Do I talk to a quest giver and that quest is the new active one? Until you figure out that, it's hard to help you.

    Once you figure that out, you'll need to have some way to reference the quest out of all your quest so that you can fetch it's data. Then, you can assign that appropriate data to the activeQuest variable.
     
  5. Calmax

    Calmax

    Joined:
    Aug 8, 2017
    Posts:
    26
    Ah ok. The way my system determines an active quest is when the player approaches a quest giver, Interacts with them with some dialogue and then the quest is added. The quests all work perfect and when the player returns to the quest giver, they are praised and given a reward.

    So far, my quests are only referenced by their name and are then added to a game object that holds them. For example, each quest giver has the quest attached to them with the quest's name referenced like so: https://imgur.com/a/8X78g3P

    Hope this gives a better idea.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    So you only have one active quest? If so, you just need to assign it's value when the quest giver grants it. If it's just referenced by name, then that's fine, just have the quest giver give the quest name, look up the data and assign it to activeQuest.