[Solved][Share]Share save script for integration

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Pixchel
Posts: 9
Joined: Tue Jun 18, 2024 11:40 am

[Solved][Share]Share save script for integration

Post 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
Last edited by Pixchel on Sun Jul 07, 2024 10:01 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Share save script for integration

Post by Tony Li »

Hi,

Yes, as long as it doesn't include Dialogue System source code, it's fine to share custom saver scripts.
Pixchel
Posts: 9
Joined: Tue Jun 18, 2024 11:40 am

Re: Share save script for integration

Post 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;
			}
			
		}
	}
}
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Share save script for integration

Post by Tony Li »

Thanks for sharing!
Post Reply