Quantcast
Channel: Answers by "ThePersister"
Viewing all articles
Browse latest Browse all 99

Answer by ThePersister

$
0
0
**[Upgraded Answer]** Hey @zentaiguy, Looking at your comments I got an idea of what you're trying to do. I made a setup of what I would do with your goal in mind, in the hope of inspiring you and helping you on your way! :) Visually (Also settable from code): ![Example image of the Dialogue's Inspector][1] The Code: using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class Dialogue : MonoBehaviour { [Header("Main")] public List m_availableQuestions = new List(); public Response m_alreadyAskedResponse = new Response(); // Add audioclip "You already said that" public Response m_silentResponse = new Response(); // Keep empty, and check for empty when trying to play the clip? [Header("KITT")] public KITT m_currentKITT = new KITT(); public Response AskRandomQuestion() { // Random Question. Question randomQuestion = m_availableQuestions.GetRandom(); // Already asked. if (randomQuestion.m_wasAlreadyAsked) { return m_alreadyAskedResponse; } else { randomQuestion.m_wasAlreadyAsked = true; } // Positive, Negative or Silent. ResponseType responseType = m_currentKITT.GetResponseType(randomQuestion); switch (responseType) { case ResponseType.Positive: return randomQuestion.m_positiveResponses.GetRandom(); case ResponseType.Negative: return randomQuestion.m_negativeResponses.GetRandom(); default: return m_silentResponse; // Silent response is default. } } } [Serializable] public class Question { // Putting the string as first item, makes it come up in the inspector as visual identifier in a list. public string m_questionString = ""; public bool m_wasAlreadyAsked = false; public List m_questionIsCompatibleWith = new List(); public List m_positiveResponses = new List(); public List m_negativeResponses = new List(); public Question() { // Compatible with all persons by default. m_questionIsCompatibleWith.AddRange( (KITTPerson[]) Enum.GetValues(typeof(KITTPerson))); } } [Serializable] public class Response { public AudioClip m_responseClip; // Add other data if necessary. } [Serializable] public class KITT { [Range(1, 10)] public int m_moodLevel = 6, // Mood from 1 to 10. m_moodThreshold = 5; // When is mood good or bad. public Mood m_currentMood = Mood.Good; public ShutUpMode m_shutUpMode = ShutUpMode.Off; public KITTPerson m_kittPerson = KITTPerson.Michael; public ResponseType GetResponseType(Question question) { bool forMe = question.m_questionIsCompatibleWith.Contains(m_kittPerson); if (m_shutUpMode == ShutUpMode.On) { return ResponseType.Silent; } else if (!forMe || m_currentMood == Mood.Bad) { _changeMoodLevel(-1); return ResponseType.Negative; } else { _changeMoodLevel(1); return ResponseType.Positive; } } public void _changeMoodLevel(int difference) { m_moodLevel = Mathf.Clamp(m_moodLevel + difference, 1, 10); m_currentMood = m_moodLevel >= m_moodThreshold ? Mood.Good : Mood.Bad; } } public enum ResponseType { Silent, Positive, Negative } public enum KITTPerson { Michael, Jimmy, Ted } public enum ShutUpMode { On, Off } public enum Mood { Good, Bad } public static class ListExtension { public static T GetRandom(this IList list) { if (list == null) { throw new System.NullReferenceException("List is null, make sure the list was initialized with = new List<>() first."); } else if (list.Count == 0) { throw new System.IndexOutOfRangeException("Cannot select a random item from an empty list"); } return list[UnityEngine.Random.Range(0, list.Count)]; } } **I hope that helps, if it did, please accept this answer, it'd be much appreciated! If you need anything else, let me know!** **Cheers,** **ThePersister** [1]: /storage/temp/82361-kitt-dialogue.png

Viewing all articles
Browse latest Browse all 99

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>