Psai Music Engine Integration (Action)

Note: this is an integration action which will allow you to control Psai Music Engine from inside AC actionlists.

There are two ways to create the script:

1 Create a blank text file, paste the code, then rename the file to the name of the class and change the extension to .cs (ActionPsai.cs).

Or

2. Create a script inside unity in the library tab named as mentioned before, then just replace the whole text in the file.

Then, just drop this in the Actions folder in AC or make your own custom Action folder, go to the Actions manager in AC and then set the folder for custom action in there.

Here is the code:

```
using UnityEngine;
using psai.net;

#if UNITY_EDITOR
using UnityEditor;
#endif

 /* Adventure Creator action class for selected Psai music engine methods */

 namespace AC
 {
     [System.Serializable]
    public class ActionPsai : Action
     {
         public enum PsaiMethod
        {
             AddToCurrentIntensity,
            CutSceneEnter,
            CutSceneLeave,
            GoToRest,
            HoldCurrentIntensity,
            MenuModeEnter,
            MenuModeLeave,
            ReturnToLastBasicMood,
            SetPaused,
            SetVolume,
            StopMusic,
            TriggerMusicTheme
        }

         public PsaiMethod selected_psai_method = PsaiMethod.AddToCurrentIntensity;

        // Parameters for Psai methods
        public float DeltaIntensity = 0f;
        public int ThemeId = 1;
        public float Intensity = 1f;
        public bool Immediately = true;
        public bool Reset = false;
        public float FadeOutSeconds = 3f;
        public bool HoldIntensity = true;
        public bool Paused = true;
        public float Volume = 1f;
        public bool SetDuration = false;
        public int MusicDurationInSeconds = 30;

        public ActionPsai()
        {
             this.isDisplayed = true;
            category = ActionCategory.ThirdParty;
            title = "Psai";
            description = "Run a Psai method";
        }

         override public float Run()
        {
             if (!PsaiCore.IsInstanceInitialized())
            {
                 Debug.LogError("Psai instance is not initialized");
                return 0f;
            }

             switch (selected_psai_method)
            {
                 case PsaiMethod.AddToCurrentIntensity:
                    return Run_AddToCurrentIntensity();

                case PsaiMethod.CutSceneEnter:
                    return Run_CutSceneEnter();

                case PsaiMethod.CutSceneLeave:
                    return Run_CutSceneLeave();

                case PsaiMethod.GoToRest:
                    return Run_GoToRest();

                case PsaiMethod.HoldCurrentIntensity:
                    return Run_HoldCurrentIntensity();

                case PsaiMethod.MenuModeEnter:
                    return Run_MenuModeEnter();

                case PsaiMethod.MenuModeLeave:
                    return Run_MenuModeLeave();

                case PsaiMethod.ReturnToLastBasicMood:
                    return Run_ReturnToLastBasicMood();

                case PsaiMethod.SetPaused:
                    return Run_SetPaused();

                case PsaiMethod.SetVolume:
                    return Run_SetVolume();

                case PsaiMethod.StopMusic:
                    return Run_StopMusic();

                case PsaiMethod.TriggerMusicTheme:
                    return Run_TriggerMusicTheme();

                default:
                    Debug.LogError("ActionPsai.cs: unknown PsaiMethod in Run");
                    return 0f;
            }
         }

         private float Run_AddToCurrentIntensity()
        {
             PsaiResult result = PsaiCore.Instance.AddToCurrentIntensity(DeltaIntensity);

            if (result != PsaiResult.OK)
            {
                 Debug.LogWarning("PsaiCore.Instance.AddToCurrentIntensity returned unexpected result: " + result.ToString());
            }

             return 0f;
        }

         private float Run_CutSceneEnter()
        {
             PsaiResult result = PsaiCore.Instance.CutSceneEnter(ThemeId, Intensity);

            switch (result)
            {
                 case PsaiResult.OK:
                    break;

                case PsaiResult.commandIgnoredMenuModeActive:
                    Debug.LogWarning("PsaiCore.Instance.CutSceneEnter: the command was ignored, call MenuModeLeave() first.");
                    break;

                case PsaiResult.commandIgnoredCutsceneActive:
                    Debug.LogWarning("PsaiCore.Instance.CutSceneEnter: the command was ignored, psai is already in Cutscene Mode.");
                    break;

                default:
                    Debug.LogWarning("PsaiCore.Instance.CutSceneEnter returned unexpected result: " + result.ToString());
                    break;
            }

             return 0f;
        }

         private float Run_CutSceneLeave()
        {
             PsaiResult result = PsaiCore.Instance.CutSceneLeave(Immediately, Reset);

            if (result != PsaiResult.OK)
            {
                 Debug.LogWarning("PsaiCore.Instance.CutSceneLeave returned unexpected result: " + result.ToString());
            }

             return 0f;
        }

         private float Run_GoToRest()
        {
             PsaiResult result = PsaiCore.Instance.GoToRest(Immediately, FadeOutSeconds);

            if (result != PsaiResult.OK)
            {
                 Debug.LogWarning("PsaiCore.Instance.GoToRest returned unexpected result: " + result.ToString());
            }

             return 0f;
        }

         private float Run_HoldCurrentIntensity()
        {
             PsaiResult result = PsaiCore.Instance.HoldCurrentIntensity(HoldIntensity);

            switch (result)
            {
                 case PsaiResult.OK:
                    break;

                case PsaiResult.commandIgnored:
                    Debug.LogWarning("PsaiCore.Instance.HoldCurrentIntensity: ignored because the intensity is already being held.");
                    break;

                case PsaiResult.commandIgnoredMenuModeActive:
                    Debug.LogWarning("PsaiCore.Instance.HoldCurrentIntensity: the command was ignored, call MenuModeLeave() first.");
                    break;

                case PsaiResult.commandIgnoredCutsceneActive:
                    Debug.LogWarning("PsaiCore.Instance.HoldCurrentIntensity: the command was ignored, call CutSceneLeave() first.");
                    break;

                default:
                    Debug.LogWarning("PsaiCore.Instance.HoldCurrentIntensity returned unexpected result: " + result.ToString());
                    break;
            }

             return 0f;
        }

         private float Run_MenuModeEnter()
        {
             PsaiResult result = PsaiCore.Instance.MenuModeEnter(ThemeId, Intensity);

            if (result != PsaiResult.OK)
            {
                 Debug.LogWarning("PsaiCore.Instance.MenuModeEnter returned unexpected result: " + result.ToString());
            }

             return 0f;
        }

         private float Run_MenuModeLeave()
        {
             PsaiResult result = PsaiCore.Instance.MenuModeLeave();

            if (result != PsaiResult.OK)
            {
                 Debug.LogWarning("PsaiCore.Instance.MenuModeLeave returned unexpected result: " + result.ToString());
            }

             return 0f;
        }

         private float Run_ReturnToLastBasicMood()
        {
             PsaiResult result = PsaiCore.Instance.ReturnToLastBasicMood(Immediately);

            switch (result)
            {
                 case PsaiResult.OK:
                    break;

                case PsaiResult.commandIgnoredMenuModeActive:
                    Debug.LogWarning("PsaiCore.Instance.ReturnToLastBasicMood: the command was ignored, call MenuModeLeave() first.");
                    break;

                case PsaiResult.commandIgnored:
                    Debug.LogWarning("PsaiCore.Instance.ReturnToLastBasicMood: the Basic Mood is already playing.");
                    break;

                default:
                    Debug.LogWarning("PsaiCore.Instance.ReturnToLastBasicMood returned unexpected result: " + result.ToString());
                    break;
            }

             return 0f;
        }

         private float Run_SetPaused()
        {
             PsaiCore.Instance.SetPaused(Paused);
            return 0f;
        }

         private float Run_SetVolume()
        {
             PsaiCore.Instance.SetVolume(Volume);
            return 0f;
        }

         private float Run_StopMusic()
        {
             PsaiResult result = PsaiCore.Instance.StopMusic(Immediately);

            if (result != PsaiResult.OK)
            {
                 Debug.LogWarning("PsaiCore.Instance.StopMusic returned unexpected result: " + result.ToString());
            }

             return 0f;
        }

         private float Run_TriggerMusicTheme()
        {
             PsaiResult result;

            if (SetDuration)
            {
                 result = PsaiCore.Instance.TriggerMusicTheme(ThemeId, Intensity, MusicDurationInSeconds);
            }
             else
            {
                 result = PsaiCore.Instance.TriggerMusicTheme(ThemeId, Intensity);
            }

             switch (result)
            {
                 case PsaiResult.OK:
                    break;

                case PsaiResult.unknown_theme:
                    Debug.LogWarning("PsaiCore.Instance.TriggerMusicTheme: the requested Theme does not exist in the current soundtrack.");
                    break;

                case PsaiResult.commandIgnoredMenuModeActive:
                    Debug.LogWarning("PsaiCore.Instance.TriggerMusicTheme: the command was ignored, call MenuModeLeave() first.");
                    break;

                default:
                    Debug.LogWarning("PsaiCore.Instance.ReturnToLastBasicMood returned unexpected result: " + result.ToString());
                    break;
            }

             return 0f;
        }

 # if UNITY_EDITOR

        override public void ShowGUI()
        {
             selected_psai_method = (PsaiMethod)EditorGUILayout.EnumPopup("Psai method", selected_psai_method);

            switch (selected_psai_method)
            {
                 case PsaiMethod.AddToCurrentIntensity:
                    ShowGUI_AddToCurrentIntensity();
                    break;

                case PsaiMethod.CutSceneEnter:
                    ShowGUI_CutSceneEnter();
                    break;

                case PsaiMethod.CutSceneLeave:
                    ShowGUI_CutSceneLeave();
                    break;

                case PsaiMethod.GoToRest:
                    ShowGUI_GoToRest();
                    break;

                case PsaiMethod.HoldCurrentIntensity:
                    ShowGUI_HoldCurrentIntensity();
                    break;

                case PsaiMethod.MenuModeEnter:
                    ShowGUI_MenuModeEnter();
                    break;

                case PsaiMethod.MenuModeLeave:
                    ShowGUI_MenuModeLeave();
                    break;

                case PsaiMethod.ReturnToLastBasicMood:
                    ShowGUI_ReturnToLastBasicMood();
                    break;

                case PsaiMethod.SetPaused:
                    ShowGUI_SetPaused();
                    break;

                case PsaiMethod.SetVolume:
                    ShowGUI_SetVolume();
                    break;

                case PsaiMethod.StopMusic:
                    ShowGUI_StopMusic();
                    break;

                case PsaiMethod.TriggerMusicTheme:
                    ShowGUI_TriggerMusicTheme();
                    break;

                default:
                    Debug.LogError("ActionPsai.cs: unknown PsaiMethod in ShowGUI");
                    break;
            }

             AfterRunningOption();
        }

         public override string SetLabel ()
        {
             string labelAdd = "Run a Psai action";
            return labelAdd;
        }

         private void ShowGUI_AddToCurrentIntensity()
        {
             DeltaIntensity = EditorGUILayout.Slider("Intensity change", DeltaIntensity, -1f, 1f);
        }

         private void ShowGUI_CutSceneEnter()
        {
             ThemeId = EditorGUILayout.IntField("Theme id", ThemeId);
            Intensity = EditorGUILayout.Slider("Intensity", Intensity, 0f, 1f);
        }

         private void ShowGUI_CutSceneLeave()
        {
             Immediately = EditorGUILayout.Toggle("Leave immediately", Immediately);
            Reset = EditorGUILayout.Toggle("Clear theme queue", Reset);
        }

         private void ShowGUI_GoToRest()
        {
             Immediately = EditorGUILayout.Toggle("Go to rest immediately", Immediately);

            if (!Immediately)
            {
                 FadeOutSeconds = EditorGUILayout.FloatField("Fade-out time (secs)", FadeOutSeconds);
            }
         }

         private void ShowGUI_HoldCurrentIntensity()
        {
             HoldIntensity = EditorGUILayout.Toggle("Hold intensity", HoldIntensity);
        }

         private void ShowGUI_MenuModeEnter()
        {
             ThemeId = EditorGUILayout.IntField("Theme id", ThemeId);
            Intensity = EditorGUILayout.Slider("Intensity", Intensity, 0f, 1f);
        }

         private void ShowGUI_MenuModeLeave()
        {
             // Nothing to set - but included for code consistency
        }

         private void ShowGUI_ReturnToLastBasicMood()
        {
             Immediately = EditorGUILayout.Toggle("Immediate return", Immediately);
        }

         private void ShowGUI_SetPaused()
        {
             Paused = EditorGUILayout.Toggle("Paused", Paused);
        }

         private void ShowGUI_SetVolume()
        {
             Volume = EditorGUILayout.Slider("Volume", Volume, 0f, 1f);
        }

         private void ShowGUI_StopMusic()
        {
             Immediately = EditorGUILayout.Toggle("Stop immediately", Immediately);
        }

         private void ShowGUI_TriggerMusicTheme()
        {
             ThemeId = EditorGUILayout.IntField("Theme id", ThemeId);
            Intensity = EditorGUILayout.Slider("Intensity", Intensity, 0f, 1f);
            SetDuration = EditorGUILayout.Toggle("Set duration", SetDuration);

            if (SetDuration)
            {
                 MusicDurationInSeconds = EditorGUILayout.IntField("Music duration (secs)", MusicDurationInSeconds);
            }
         }
 #endif
     }
 }

```

[Code provided by user: Snebjorn]