Page 1 of 1

[Solved][Share]Share save script for integration

Posted: Sat Jul 06, 2024 2:51 am
by Pixchel
Hello,

I come here to ask something about a saver script for invector.
I asked you on discord but without answer from you I want to ask it here too, to be sure to have the right to share a saver for Invector craft system that save/load learned recipe ?

If you are Ok, i'll share it on Invector discord ressources sharing, and here too... if you want to add it in the invector integration in the future.

Best regards. Pixchel

Re: Share save script for integration

Posted: Sat Jul 06, 2024 8:50 am
by Tony Li
Hi,

Yes, as long as it doesn't include Dialogue System source code, it's fine to share custom saver scripts.

Re: Share save script for integration

Posted: Sat Jul 06, 2024 2:39 pm
by Pixchel
I past here the script :

Code: Select all

using Invector.vItemManager;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

namespace PixelCrushers.DialogueSystem
{
	/// <summary>
    /// Save System saver for Invector Craft Recipe.
    /// </summary>
    [AddComponentMenu("Pixel Crushers/Save System/Savers/Invector Craft System/Recipes Saver")]
	public class InvectorCraftingControllerSaver : Saver
	{
		public static bool isResettingScene = false;
		
		//Used to visualize in inspector
		public List<SavedRecipe> tempCraftableItems;
		[Serializable]
        public class Data
        {
            public List<SavedRecipe> savedRecipes = new List<SavedRecipe>();
        }
		
		public vItemCraftControl itemCraftControl;
		private Data m_data;
		
		void Awake()
		{
			if(!itemCraftControl)
			{
				itemCraftControl = GetComponent<vItemCraftControl>();
			}
			m_data = new Data();
		}
		
		public override string RecordData()
        {
			if(itemCraftControl.craftableItems == null) return string.Empty;
			if(itemCraftControl.craftableItems != null)
			{
				if(itemCraftControl.craftableItems.Count > 0)
				{
					if(tempCraftableItems.Count > 0)
					{
						tempCraftableItems.Clear();
					}
					for (int i = 0; i < itemCraftControl.craftableItems.Count; i++)
					{
						//Used to visualize in inspector
						tempCraftableItems.Add(new SavedRecipe(itemCraftControl.craftableItems[i].item.id, itemCraftControl.craftableItems[i].canCraft));
						m_data.savedRecipes.Add(new SavedRecipe(itemCraftControl.craftableItems[i].item.id, itemCraftControl.craftableItems[i].canCraft));
					}
				}
			}
			
			return SaveSystem.Serialize(m_data);
        }
		
		public override void ApplyData(string s)
        {
			if (string.IsNullOrEmpty(s)) return;

            var data = SaveSystem.Deserialize<Data>(s);
            if (data == null) return;
			
			foreach(var globalRecipe in data.savedRecipes)
			{
				itemCraftControl.SetActiveCanCraftItem(globalRecipe.sItemID, globalRecipe.sCanCraft);
			}
		}
		
		[System.Serializable]
		public class SavedRecipe
		{
			public int sItemID;
			public bool sCanCraft;
			public SavedRecipe(int item, bool canCraft)
			{
				this.sItemID = item;
				this.sCanCraft = canCraft;
			}
			
		}
	}
}

Re: Share save script for integration

Posted: Sat Jul 06, 2024 6:27 pm
by Tony Li
Thanks for sharing!