Unable to save/load data

Announcements, support questions, and discussion for Quest Machine.
EVG
Posts: 27
Joined: Sun Jul 04, 2021 4:55 pm

Re: Unable to save/load data

Post by EVG »

Tony Li wrote: Thu Dec 22, 2022 11:38 am Hi,

Regarding the QuestControl error message, this is a bug in Unity 2022. This patch has a temporary fix:

PixelCrushers_HelpAttributePatch_2022-12-22.unitypackage

Regarding your custom quest condition, QuestCondition is not a MonoBehaviour. You can't count on Awake() or OnEnable(). Can you rewrite it to eliminate those methods?
I removed Awake() and OnEnable(), but the save still doesn't work.

Code: Select all

using Opsive.Shared.Events;
using Opsive.UltimateInventorySystem.Core;
using PixelCrushers.QuestMachine;
using System.Collections;
using UnityEngine;

[System.Serializable]
public class ActiveEquippedItemQuestCondition : QuestCondition
{
    [SerializeField] private ItemDefinition activeEquippedItemDefinition = default;

    private void OnEquipItemComplete(Opsive.UltimateCharacterController.Items.Item item, int arg2)
    {
        if (quest.GetState() == QuestState.Successful)// || questNode.GetState() == QuestNodeState.Inactive)
        {
            return;
        }
        if (item.ItemDefinition == activeEquippedItemDefinition)
        {
            QuestMachineConfiguration.instance.StartCoroutine(StateSetTrue());
        }
    }

    private void OnUnequipItemComplete(Opsive.UltimateCharacterController.Items.Item item, int arg2)
    {
        if (quest.GetState() == QuestState.Successful)// || questNode.GetState() == QuestNodeState.Inactive)
        {
            return;
        }
        if (item.ItemDefinition == activeEquippedItemDefinition)
        {
            SetNodeInactive();
        }
    }

    private void OnInventoryRemoveItem(Opsive.UltimateCharacterController.Items.Item item, int arg2)
    {
        if (quest.GetState() == QuestState.Successful)// || questNode.GetState() == QuestNodeState.Inactive)
        {
            return;
        }
        if (item.ItemDefinition == activeEquippedItemDefinition)
        {
            SetNodeInactive();
        }
    }

    private void SetNodeInactive()
    {
        foreach (QuestNode child in questNode.childList)
        {
            child.SetConditionChecking(false);
            child.SetState(QuestNodeState.Inactive);
        }

        questNode.ResetConditions();
        questNode.SetState(QuestNodeState.Active);
    }

    private IEnumerator StateSetTrue()
    {
        yield return null;
        SetTrue();
    }

    public override string GetEditorName()
    {
        return "Check Active Equipped Item: " + activeEquippedItemDefinition;
    }

    public override void StartChecking(System.Action trueAction)
    {
        if (GlobalCache.Instance != null && GlobalCache.Instance.HumanCharacter != null)
        {
            EventHandler.RegisterEvent<Opsive.UltimateCharacterController.Items.Item, int>(GlobalCache.Instance.HumanCharacter, "OnInventoryEquipItem", OnEquipItemComplete);
            EventHandler.RegisterEvent<Opsive.UltimateCharacterController.Items.Item, int>(GlobalCache.Instance.HumanCharacter, "OnAbilityUnequipItemComplete", OnUnequipItemComplete);
            EventHandler.RegisterEvent<Opsive.UltimateCharacterController.Items.Item, int>(GlobalCache.Instance.HumanCharacter, "OnInventoryRemoveItem", OnInventoryRemoveItem);
        }

        base.StartChecking(trueAction);
        if (IsTrue()) SetTrue();
        quest.stateChanged += OnQuestStateChanged;
    }

    private bool IsTrue()
    {
        var activeEquippedItemInSlot0 = GlobalCache.Instance.CharacterInventoryBridge.GetActiveInventoryItem(0);
        if (activeEquippedItemInSlot0 != null)
        {
            if (activeEquippedItemInSlot0.GetItemDefinition() == activeEquippedItemDefinition)
            {
                return true;
            }
        }
        return false;
    }

    private void OnQuestStateChanged(Quest quest)
    {
        if (quest.GetState() == QuestState.Successful)
        {
            if (GlobalCache.Instance != null && GlobalCache.Instance.HumanCharacter != null)
            {
                EventHandler.UnregisterEvent<Opsive.UltimateCharacterController.Items.Item, int>(GlobalCache.Instance.HumanCharacter, "OnInventoryEquipItem", OnEquipItemComplete);
                EventHandler.UnregisterEvent<Opsive.UltimateCharacterController.Items.Item, int>(GlobalCache.Instance.HumanCharacter, "OnAbilityUnequipItemComplete", OnUnequipItemComplete);
                EventHandler.UnregisterEvent<Opsive.UltimateCharacterController.Items.Item, int>(GlobalCache.Instance.HumanCharacter, "OnInventoryRemoveItem", OnInventoryRemoveItem);
            }
        }
    }
}
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unable to save/load data

Post by Tony Li »

Can you send a reproduction project with reproduction steps to tony (at) pixelcrushers.com?
EVG
Posts: 27
Joined: Sun Jul 04, 2021 4:55 pm

Re: Unable to save/load data

Post by EVG »

Tony Li wrote: Fri Dec 23, 2022 10:03 am Can you send a reproduction project with reproduction steps to tony (at) pixelcrushers.com?
I found the reason. The problem was that immediately after completing the quest, I turned off the game object with the Quest Giver component, but I didn’t need to do this. In any case, thanks for the help. :)
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Unable to save/load data

Post by Tony Li »

Got it. I'm glad you found the issue. You can save the active/inactive states of GameObjects, btw, if you want to. Use an ActiveSaver or MultiActiveSaver.
EVG
Posts: 27
Joined: Sun Jul 04, 2021 4:55 pm

Re: Unable to save/load data

Post by EVG »

Tony Li wrote: Fri Dec 23, 2022 3:00 pm Got it. I'm glad you found the issue. You can save the active/inactive states of GameObjects, btw, if you want to. Use an ActiveSaver or MultiActiveSaver.
I use them too, thanks!
Post Reply