Add the Cinematic Played State in your save file

Here is a quick and easy fix to all cinematics “Played State” not resetting every time you go to a diferent scene

Step 1

  • Add the “SaveableEntity.cs” to your Intro Sequence (or other cinematics you have)

Step 2

  • In your “CinematicTrigger.cs” add the ISavalbe interface and create the 2 methods like this
namespace RPG.Cinematics
{
    public class CinematicTrigger : MonoBehaviour, ISaveable
    {
        private bool wasPlayed = false;
        private void OnTriggerEnter(Collider other)
        {
            if (!wasPlayed && other.gameObject.CompareTag("Player"))
            {
                wasPlayed = true;
                GetComponent<PlayableDirector>().Play();
            }
        }

        public object CaptureState()
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            data["Cinematic"] = wasPlayed;
            return data;
        }

        public void RestoreState(object state)
        {
            Dictionary<string, object> data = (Dictionary<string, object>)state;
            wasPlayed = (bool)data["Cinematic"];
            Debug.Log("Restored "+wasPlayed);
        }
    }

}
5 Likes

Fantastic job :raised_hands:

1 Like

Awesome tip!

1 Like

thanks for the share!

1 Like

I did something similar with mine. But in the restore state I check if it was played and play it change the time to the duration of the playable director.

I do this because my cut scene currently activates my patrolling enemies. I my change this in the future.

using RPG.Core;
using RPG.Saving;
using UnityEngine;
using UnityEngine.Playables;

namespace RPG.Cinematics
{
   [RequireComponent(typeof(PlayableDirector), typeof(SaveableEntity))]
   public class CinematicTrigger : MonoBehaviour, ISaveable
   {
      // #region Component References

       [ReadOnly, SerializeField] private bool triggered;

      // #region Unity Messages

       #region Implementation of ISaveable

       /// <inheritdoc />
       public object CaptureState()
       {
           return triggered;
       }

       /// <inheritdoc />
       public void RestoreState(object state)
       {
           triggered = (bool)state;
           if (!triggered) return;
           m_director.Play();
           m_director.time = m_director.duration;
       }

       #endregion
   }
}
1 Like

I have added an option to my CinematicTrigger that allows defining cut-scenes to be one-offs or being played repeatedly. The ISaveable methods need a slight adjustmet in order to accomodate for that.

        public object CaptureState()
        {
            if (!isOnetimeTrigger) return true;

            return GetComponent<BoxCollider>().enabled;
        }


        public void RestoreState(object state)
        {
            if (!isOnetimeTrigger) return;

            if (state is bool newState)
            {
                GetComponent<BoxCollider>().enabled = newState;
            }
            else
            {
                Debug.LogError($"{name}.{this.GetType()} loaded invalid data.");
                return;
            }
        }
1 Like

Privacy & Terms