Playing speech from ID

This custom Action allows you to play speech that has already been gathered in the Speech Manager, allowing you to recycle an existing speech line's text, translation, and audio.

To use it, install the script below as a custom Action named ActionSpeechID.cs. For more on custom Actions, see this tutorial. Then assign both a speaking character, and a line number that's already listed in the Speech Manager.

ActionSpeechID.cs:

```
using System.Collections.Generic;
using UnityEngine;
#if UNITY\_EDITOR
using UnityEditor;
#endif

namespace AC
{

 [System.Serializable]
 public class ActionSpeechID : ActionSpeech
 {

 public int sharedLineID;
 public int sharedLineParameterID = -1;
 private SpeechLine sharedSpeechLine;

 public override string Title { get { return "Play speech ID"; }}
 public override string Description { get { return "Plays dialogue already gathered in the Speech Manager from another 'Dialogue: Play speech' Action."; }}

 public override void AssignValues (List<ActionParameter> parameters)
 {
 speech = null;
 runtimeSpeaker = null;

 if (parameters != null) ownParameters = parameters;

 sharedLineID = AssignInteger (parameters, sharedLineParameterID, sharedLineID);

 sharedSpeechLine = KickStarter.speechManager.GetLine (sharedLineID);
 if (sharedSpeechLine != null)
 {
 messageText = KickStarter.runtimeLanguages.GetTranslation (sharedSpeechLine.text, sharedLineID, Options.GetLanguage ());
 lineID = sharedLineID;

 if (sharedSpeechLine.isPlayer)
 {
 runtimeSpeaker = KickStarter.player;
 }
 else
 {
 runtimeSpeaker = GetCharacterWithName (sharedSpeechLine.owner);
 }
 }
 }

 override public void AssignParentList (ActionList actionList)
 {
 if (actionList != null)
 {
 localVariables = UnityVersionHandler.GetLocalVariablesOfGameObject (actionList.gameObject);
 runActionListInBackground = (actionList.actionListType == ActionListType.RunInBackground);
 }
 if (localVariables == null)
 {
 localVariables = KickStarter.localVariables;
 }

 base.AssignParentList (actionList);
 }

 private AC.Char GetCharacterWithName (string charName)
 {
 if (Application.isPlaying)
 {
 foreach (AC.Char character in KickStarter.stateHandler.Characters)
 {
 if (character.name == charName)
 {
 return character;
 }
 }
 }
 else
 {
 AC.Char[] characters = Object.FindObjectsOfType <AC.Char>();
 foreach (AC.Char character in characters)
 {
 if (character.name == charName)
 {
 return character;
 }
 }
 }
 return null;
 }

 #if UNITY\_EDITOR

 override public void ShowGUI (List<ActionParameter> parameters)
 {
 sharedLineParameterID = ChooseParameterGUI ("Line ID:", parameters, sharedLineParameterID, ParameterType.Integer);
 if (sharedLineParameterID >= 0)
 {
 sharedLineID = 0;
 }
 else
 {
 sharedLineID = EditorGUILayout.DelayedIntField ("Line ID:", sharedLineID);
 }

 if (sharedLineID <= 0)
 {
 sharedSpeechLine = null;
 }
 else if ((GUI.changed || sharedSpeechLine == null) && KickStarter.speechManager != null)
 {
 sharedSpeechLine = KickStarter.speechManager.GetLine (sharedLineID);
 }

 if (sharedSpeechLine != null)
 {
 if (sharedSpeechLine.textType == AC\_TextType.Speech)
 {
 ShowField ("Speaker:", sharedSpeechLine.owner, false);
 ShowField ("Line text:", sharedSpeechLine.text, true);

 if (sharedSpeechLine.isPlayer)
 {
 if (Application.isPlaying)
 {
 speaker = KickStarter.player;
 }
 else if (AdvGame.GetReferences ().settingsManager)
 {
 speaker = AdvGame.GetReferences ().settingsManager.GetDefaultPlayer ();
 }
 }
 else
 {
 speaker = GetCharacterWithName (sharedSpeechLine.owner);
 }

 if (speaker != null)
 {
 if (speaker.GetAnimEngine ())
 {
 speaker.GetAnimEngine ().ActionSpeechGUI (this, speaker);
 }
 }
 }
 else
 {
 EditorGUILayout.HelpBox ("Incorrect type. Line with ID=" + sharedLineID + " is of type " + sharedSpeechLine.textType.ToString (), MessageType.Warning);
 }
 }
 else if (sharedLineID > 0)
 {
 EditorGUILayout.HelpBox ("No line found with ID=" + sharedLineID, MessageType.Warning);
 }

 isBackground = EditorGUILayout.Toggle ("Play in background?", isBackground);
 }

 private void ShowField (string label, string field, bool multiLine)
 {
 if (multiLine)
 {
 GUIStyle style = new GUIStyle ();
 style.wordWrap = true;
 if (EditorGUIUtility.isProSkin)
 {
 style.normal.textColor = new Color (0.8f, 0.8f, 0.8f);
 }
 style.alignment = TextAnchor.MiddleLeft;
 EditorGUILayout.LabelField (label, field, style);
 }
 else
 {
 EditorGUILayout.LabelField (label, field);
 }
 }

 public override string SetLabel ()
 {
 if (sharedLineID > 0)
 {
 return sharedLineID.ToString ();
 }
 return string.Empty;
 }

 public new int GetNumTranslatables ()
 {
 return 0;
 }

 #endif

 }

}

```