trying to expand save extra info of level and position of ea

Discussion and help for Easy Save 3
Post Reply
wethecom
Posts: 12
Joined: Sun Aug 16, 2015 7:39 pm

trying to expand save extra info of level and position of ea

Post by wethecom »

this is a functioning code to save and load prefabs positions.. it works well its taken from the demos
now i need to ..for each object record its level along with its position ill be able to do just about anything..
any ideas how to expand this so save a int level value for each i would be grateful this will help me understand
ill be working at this now

Code: Select all


// A List which we'll add the Transforms of our created prefabs to.
	public List<Transform> prefabInstances = new List<Transform>();

public void Save()
	{
		// Save the number of created prefabs there are.
		ES3.Save<int>(id+"count", prefabInstances.Count);

		// Save our Transforms.
		ES3.Save<List<Transform>>(id, prefabInstances);
        // save and load the level of the tower

	}

	/*
	 * 	We will call this after downloading the data from the server.
	 */
	public void Load() 
	{
        
		// Load how many prefabs we need to instantiate.
		int count = ES3.Load<int>(id + "count", 0);
		// If there are prefabs to load, load them.
		if(count > 0)
		{
          
            // For each prefab we want to load, instantiate a prefab.
            for (int i = 0; i < count; i++)

                InstantiatePrefab(0);// the 0 is meant to be the level when created set the level.. it set to 0 because its not implemented yet
           
			// Load our List of Transforms into our prefab array.
			ES3.LoadInto<List<Transform>>(id, prefabInstances);
		}
	}


public GameObject InstantiatePrefab(int Level)
	{///old demo code
		var go = Instantiate(prefab);
		prefabInstances.Add(go.transform);
        go.GetComponent<UnitTower>().level = Level;
		return go;
	}

Last edited by wethecom on Mon Dec 17, 2018 4:54 am, edited 3 times in total.
wethecom
Posts: 12
Joined: Sun Aug 16, 2015 7:39 pm

Re: trying to expand not sure what im doing

Post by wethecom »

ok this is what i got so far..
throws an error ..its not reading it right
stuck where i was last time i tried this

Code: Select all

	public string id = System.Guid.NewGuid().ToString();

    public string TowerName;

	// A List which we'll add the Transforms of our created prefabs to.
	public List<Transform> prefabInstances = new List<Transform>();
    public List<int> prefabInstancesLevel = new List<int>();

    /*
	 * This is where we will save our data.
	 * This is called by the 'Save and Upload' button. 
	 */
    public void Save()
	{
		// Save the number of created prefabs there are.
		ES3.Save<int>(id+"count", prefabInstances.Count);
        
        for (int i = 0; i < prefabInstances.Count; i++)
        {
            
            try
            {
                int j;
                j = prefabInstances[i].GetComponent<UnitTower>().level;
                ES3.Save<int>(i + "Level", prefabInstancesLevel[j]);
                prefabInstancesLevel.Add(j);
                // throw new System.ArgumentException("Invalid x argument");
            }
            catch (System.ArgumentException ex) //when ()
            {
                // This is reached.
                Debug.Log(ex.ToString());
            }
        }
        // Save our Transforms.
       
        ES3.Save<List<Transform>>(id, prefabInstances );
        // save and load the level of the tower

	}

	/*
	 * 	We will call this after downloading the data from the server.
	 */
	public void Load() 
	{
        
		// Load how many prefabs we need to instantiate.
		int count = ES3.Load<int>(id + "count", 0);
		// If there are prefabs to load, load them.
		if(count > 0)
		{
          
            // For each prefab we want to load, instantiate a prefab.
            for (int i = 0; i < count; i++)
                //both generates a prefabe and sets its level 
                try
                {
                    int j;
                  j=  ES3.Load<int>(i + "level", prefabInstancesLevel[i]);
                    Debug.Log(prefabInstancesLevel[j]);
                    InstantiatePrefab(prefabInstancesLevel[j]);
                    
                    // throw new System.ArgumentException("Invalid x argument");
                }
                catch (System.ArgumentException ex) //when ()
                {
                    // This is reached.
                    Debug.Log(ex.ToString());
                }
           
          
            // Load our List of Transforms into our prefab array.
            ES3.LoadInto<List<Transform>>(id, prefabInstances);
            //ES3.LoadInto<List<int>>(id + "level", prefabInstancesLevel);

        }
	}

    /*
	 * 	Creates an instance of the prefab and adds it to the instance list.
	 * 	You should call this instead of Unity's Instantiate method.
	 */


	public GameObject InstantiatePrefab(int Level)
	{///old demo code
		var go = Instantiate(prefab);
		prefabInstances.Add(go.transform);
        go.GetComponent<UnitTower>().level = Level;
		return go;
	}

	/*

User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: trying to expand save extra info of level and position o

Post by Joel »

Hi there,

Before saving you would need to put your level numbers into a List<int> and save this. And then after loading and creating your prefab instances (i.e. at the very end of your Load method), you would load this array and then assign the level numbers to each prefab instance.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
wethecom
Posts: 12
Joined: Sun Aug 16, 2015 7:39 pm

Re: trying to expand save extra info of level and position o

Post by wethecom »

ok i broke it down to its simplest form
and it errors
i modify the list by hand and save it , i have buttons to save and load

FormatException: Expected '}', found '1'.
ES3Internal.ES3JSONReader.ReadCharIgnoreWhitespace (System.Char expectedChar) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:407)
ES3Internal.ES3JSONReader.EndReadObject () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:113)
ES3Internal.ES3JSONReader.ReadKeySuffix () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:102)
ES3Reader.ReadInto[T] (System.String key, T obj) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:196)
ES3.LoadInto[T] (System.String key, T obj, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:351)
ES3.LoadInto[T] (System.String key, T obj) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:318)
ListTests.Load () (at Assets/ListTests.cs:24)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ListTests : MonoBehaviour {
   
    public string idLvl = System.Guid.NewGuid().ToString();
   
    public List<int> prefabInstancesLvl = new List<int>();
    // Use this for initialization
    public void Save()
    {
       
        ES3.Save<List<int>>(idLvl, prefabInstancesLvl);
       
    }
    public void Load()
    {

            ES3.LoadInto<List<int>>(idLvl, prefabInstancesLvl);
        
    }




Last edited by wethecom on Tue Dec 18, 2018 8:54 pm, edited 2 times in total.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: trying to expand save extra info of level and position o

Post by Joel »

Hi there,

Ints are not reference types, so you can not LoadInto them. Instead you should simply use ES3.Load and assign it back to the variable. i.e.
prefabInstancesLvl = ES3.Load<List<int>>(idLvl);
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
wethecom
Posts: 12
Joined: Sun Aug 16, 2015 7:39 pm

Re: trying to expand save extra info of level and position o

Post by wethecom »

there seem to be a problem saveing the data ..see the 1.. i tried your code but i get the same error

FormatException: Expected '}', found '1'.
here is the json data
{"1c3b72fe-8df0-46e3-b79a-1e7cb6f1fa69":{"__type":"System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],mscorlib","value":[1,2,3]}}


Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ListTests : MonoBehaviour {
   
    public string idLvl = System.Guid.NewGuid().ToString();
   
    public List<int> prefabInstancesLvl = new List<int>();
    // Use this for initialization
    public void Save()
    {
       
        ES3.Save<List<int>>(idLvl, prefabInstancesLvl);
       
    }
    public void Load()
    {

        prefabInstancesLvl = ES3.Load<List<int>>(idLvl);
        //  ES3.LoadInto<List<int>>(idLvl, prefabInstancesLvl);

    }



    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

Joel wrote:Hi there,

Ints are not reference types, so you can not LoadInto them. Instead you should simply use ES3.Load and assign it back to the variable. i.e.
prefabInstancesLvl = ES3.Load<List<int>>(idLvl);
All the best,
Joel
wethecom
Posts: 12
Joined: Sun Aug 16, 2015 7:39 pm

Re: trying to expand save extra info of level and position o

Post by wethecom »

ok i had an issue after reinstall, it all works this solved it thank you
Post Reply