Adventure Creator Wikia

This custom Action, "Dialogue: Start conversation (one-off)", allows you to create a Conversation and its options within the Action itself - no need to create a separate Conversation object:

ActionConversationOneOff.cs:

using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

    public class ActionConversationOneOff : ActionConversation, ITranslatable
	{

		public List<LabelData> optionLabels = new List<LabelData> ();

		public override string Title { get { return "Start Conversation (one-off)"; }}
		public override int NumSockets { get { return optionLabels.Count; }}


		public override void AssignValues (List<ActionParameter> parameters)
		{
			overrideOptions = true;
			willWait = false;
			if (optionLabels.Count == 0) optionLabels.Add (new LabelData (0, GetNewID ()));

			if (conversation == null)
			{
				conversation = parentActionList.gameObject.AddComponent<Conversation> ();
				foreach (LabelData optionLabel in optionLabels)
				{
					ButtonDialog buttonDialog = new ButtonDialog (optionLabel.ID, optionLabel.label, true);
					buttonDialog.lineID = optionLabel.lineID;
					conversation.options.Add (buttonDialog);
				}
			}
			runtimeConversation = conversation;
		}


		#if UNITY_EDITOR

		public override void ShowGUI (List<ActionParameter> parameters)
		{
			if (optionLabels.Count == 0) optionLabels.Add (new LabelData (0, GetNewID ()));

			for (int i = 0; i < optionLabels.Count; i++)
			{
				EditorGUILayout.BeginHorizontal ();
				optionLabels[i].label = EditorGUILayout.TextField (optionLabels[i].label);

				if (GUILayout.Button (string.Empty, CustomStyles.IconCog))
				{
					SideMenu (i);
				}
				EditorGUILayout.EndHorizontal ();
			}

			if (GUILayout.Button ("Add new"))
			{
				optionLabels.Add (new LabelData (optionLabels.Count, GetNewID ()));
			}
		}


		private int sideMenuIndex;

		private void SideMenu (int index)
		{
			GenericMenu menu = new GenericMenu ();
			sideMenuIndex = index;
			if (optionLabels.Count > 0)
			{
				menu.AddItem (new GUIContent ("Delete"), false, SideMenu, "Delete");
			}
			menu.ShowAsContext ();
		}


		private void SideMenu (object obj)
		{
			switch (obj.ToString ())
			{
				case "Delete":
					optionLabels.RemoveAt (sideMenuIndex);
					endings.RemoveAt (sideMenuIndex);
					break;

				default:
					break;
			}
		}


		protected override string GetSocketLabel (int i)
		{
			return optionLabels[i].label;
		}

		#endif

		private int GetNewID ()
		{
			List<int> idArray = new List<int>();
			foreach (LabelData optionLabel in optionLabels)
			{
				idArray.Add (optionLabel.ID);
			}
			idArray.Sort ();

			int newID = 0;
			foreach (int _id in idArray)
			{
				if (newID == _id)
					newID ++;
			}
			return newID;
		}


		#region ITranslatable

		public string GetTranslatableString (int index)
		{
			if (index < optionLabels.Count)
			{
				return optionLabels[index].label;
			}
			return string.Empty;
		}


		public int GetTranslationID (int index)
		{
			if (index < optionLabels.Count)
			{
				return optionLabels[index].lineID;
			}
			return -1;
		}


		#if UNITY_EDITOR

		public void UpdateTranslatableString (int index, string updatedText)
		{
			if (index < optionLabels.Count)
			{
				optionLabels[index].label = updatedText;
			}
		}


		public int GetNumTranslatables ()
		{
			return optionLabels.Count;
		}


		public bool HasExistingTranslation (int index)
		{
			if (index < optionLabels.Count)
			{
				return (optionLabels[index].lineID > -1);
			}
			return false;
		}


		public void SetTranslationID (int index, int _lineID)
		{
			if (index < optionLabels.Count)
			{
				optionLabels[index].lineID = _lineID;
			}
		}


		public string GetOwner (int index)
		{
			return string.Empty;
		}


		public bool OwnerIsPlayer (int index)
		{
			return false;
		}


		public AC_TextType GetTranslationType (int index)
		{
			return AC_TextType.DialogueOption;
		}


		public bool CanTranslate (int index)
		{
			if (index < optionLabels.Count)
			{
				return !string.IsNullOrEmpty (optionLabels[index].label);
			}
			return false;
		}

		#endif

		#endregion


		[Serializable]
		public class LabelData
		{

			public int lineID = -1;
			public string label;
			public int ID;


			public LabelData (int index, int _ID)
			{
				lineID = -1;
				label = "Option " + (index+1);
				ID = _ID;
			}


			public LabelData (string _label, int _ID)
			{
				lineID = -1;
				label = _label;
				ID = _ID;
			}

		}


		public static ActionConversationOneOff CreateNew (string[] labels)
		{
			ActionConversationOneOff newAction = CreateNew<ActionConversationOneOff> ();
			newAction.optionLabels = new List<LabelData> ();
			
			for (int i = 0; i < labels.Length; i++)
			{
				newAction.optionLabels.Add (new LabelData (labels[i], i));
			}
			
			return newAction;
		}


	}

}