Stupid Continue button

Share custom commands, script functions, actor implementations and other Naninovel plug-ins you've created.
Post Reply
KVinS
Posts: 1
Joined: 31 Mar 2024 01:39

Stupid Continue button

Post by KVinS »

Am I the only one who was bothered by the fact that the “CONTINUE” button works like “LOAD”?

While I don’t understand the internal structure of NaniNovel well, the button takes on more powers than it should (PlayTitleLoad). But even though it's stupid, it works.

If you have ideas or comments on the code, welcome:

Code: Select all

using UnityEngine;

namespace Naninovel.UI
{
    public class TitleAutoContinueButton : ScriptableButton
    {
        private IStateManager stateManager;
        private IScriptPlayer scriptPlayer;
        private IScriptManager scriptManager;
        private ISaveSlotManager<GameStateMap> slotManager => stateManager?.GameSlotManager;
        
        protected override void Awake ()
        {
            base.Awake();

            stateManager = Engine.GetService<IStateManager>();
            scriptPlayer = Engine.GetService<IScriptPlayer>();
            scriptManager = Engine.GetService<IScriptManager>();
        }

        protected override void Start ()
        {
            base.Start();

            ControlInteractability(default);
        }

        protected override void OnEnable ()
        {
            base.OnEnable();

            stateManager.GameSlotManager.OnSaved += ControlInteractability;
            stateManager.GameSlotManager.OnDeleted += ControlInteractability;
        }

        protected override void OnDisable ()
        {
            base.OnDisable();

            stateManager.GameSlotManager.OnSaved -= ControlInteractability;
            stateManager.GameSlotManager.OnDeleted -= ControlInteractability;
        }

        protected override void OnButtonClick ()
        {
            HandleLoadSlot();
        }
        
        protected virtual async void HandleLoadSlot ()
        {
            string lastSaveId = null;
            GameStateMap lastSave = null;
            
            for (int slotNumber = 0; slotNumber < stateManager.Configuration.SaveSlotLimit; slotNumber++)
            {
                var slotId = stateManager.Configuration.IndexToSaveSlotId(slotNumber);
                if (!slotManager.SaveSlotExists(slotId)) continue;
                var state = await slotManager.LoadAsync(slotId);
                if (lastSave == null || lastSave.SaveDateTime < state.SaveDateTime)
                {
                    lastSaveId = slotId;
                    lastSave = state;
                }
            }

            for (int slotNumber = 0; slotNumber < stateManager.Configuration.QuickSaveSlotLimit; slotNumber++ )
            {
                var slotId = stateManager.Configuration.IndexToQuickSaveSlotId(slotNumber);
                if (!slotManager.SaveSlotExists(slotId)) continue;
                var state = await slotManager.LoadAsync(slotId);
                if (lastSave == null || lastSave.SaveDateTime < state.SaveDateTime)
                {
                    lastSaveId = slotId;
                    lastSave = state;
                }
            }

            if (!slotManager.SaveSlotExists(lastSaveId)) return;
            await PlayTitleLoad();
            using (await LoadingScreen.ShowAsync())
            {
                Hide();
                Engine.GetService<IUIManager>()?.GetUI<ITitleUI>()?.Hide();
                await stateManager.LoadGameAsync(lastSaveId);
            }
        }

        private void ControlInteractability (string _) => UIComponent.interactable = stateManager.AnyGameSaveExists;
        
        protected virtual async UniTask PlayTitleLoad ()
        {
            const string label = "OnLoad";
            var scriptName = scriptManager.Configuration.TitleScript;
            if (!scriptManager.TryGetScript(scriptName, out var script) || !script.LabelExists(label)) return;
            scriptPlayer.ResetService();
            await scriptPlayer.PreloadAndPlayAsync(scriptName, label: label);
            await UniTask.WaitWhile(() => scriptPlayer.Playing);
        }
    }
}
Post Reply