ES2Writer/Reader with character list

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
jinxology
Posts: 35
Joined: Sat Sep 13, 2014 5:21 pm

ES2Writer/Reader with character list

Post by jinxology »

Greetings,

My game allows the player to create several characters, but I'm not sure the best approach here. I looked into ES2Writer, and that functionality seems to be working in the Web Player. How should I structure the save though? Should each character be a separate "text file"? Is there a way to save an array of Character objects so that I can just read them all in easily? Also, is a text file really used when using ES2Writer in the Web Player?
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES2Writer/Reader with character list

Post by Joel »

Hi there,

The ES2.Save/ES2.Load methods support Web Player also, and would be much easier than using ES2Writer.

Because you can't save to file on Web Player, it will save the data to PlayerPrefs by default, but still accept a filename so that it's cross-compatible with other platforms. Note that Unity limits Web Player storage to 1MB.

You can really structure the save however you want, either by using different files for each character or by using Tags and appending the character name to the Tag (or something similar).

There is a guide for adding support for custom types here. If you add support for a type, you will also be able to save an array of that type using ES2.Save and ES2.LoadArray.

All the best,
Joel
jinxology
Posts: 35
Joined: Sat Sep 13, 2014 5:21 pm

Re: ES2Writer/Reader with character list

Post by jinxology »

I would love to support the type, but when I use the tool at Assets > Easy Save 2 > Manage Types... Choose "Character" check all the boxes, I get this error on compile:

Code: Select all

Assets/Easy Save 2/Types/ES2UserType_Character.cs(49,52): error CS0246: The type or namespace name `Character' could not be found. Are you missing a using directive or an assembly reference?
Here's the file it generated. The error is on the last line:

public ES2UserType_Character():base(typeof(Character)){}

Is it because my whole project is in Javascript? Maybe it's trying to compile this first?

Full code:

Code: Select all

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


public class ES2UserType_Character : ES2Type
{
	public override void Write(object obj, ES2Writer writer)
	{
		Character data = (Character)obj;
		// Add your writer.Write calls here.
		writer.Write(data.myName);
		writer.Write(data.race);
		writer.Write(data.myClass);
		writer.Write(data.str);
		writer.Write(data.myInt);
		writer.Write(data.dex);
		writer.Write(data.con);
		writer.Write(data.luck);
		writer.Write(data.hp);
		writer.Write(data.hpCurrent);
		writer.Write(data.mana);
		writer.Write(data.manaCurrent);

	}
	
	public override object Read(ES2Reader reader)
	{
		Character data = new Character();
		// Add your reader.Read calls here and return your object.
		data.myName = reader.Read<System.String>();
		data.race = reader.Read<System.Int32>();
		data.myClass = reader.Read<System.Int32>();
		data.str = reader.Read<System.Int32>();
		data.myInt = reader.Read<System.Int32>();
		data.dex = reader.Read<System.Int32>();
		data.con = reader.Read<System.Int32>();
		data.luck = reader.Read<System.Int32>();
		data.hp = reader.Read<System.Int32>();
		data.hpCurrent = reader.Read<System.Int32>();
		data.mana = reader.Read<System.Int32>();
		data.manaCurrent = reader.Read<System.Int32>();

		return data;
	}
	
	/* ! Don't modify anything below this line ! */
	public ES2UserType_Character():base(typeof(Character)){}  
}
jinxology
Posts: 35
Joined: Sat Sep 13, 2014 5:21 pm

Re: ES2Writer/Reader with character list

Post by jinxology »

Ok, adding to this. I went and created a class in C#, placed it in the Plugins folder, and the missing class error has gone away. Hoping that solves all my issues.

Based on your earlier email, are you suggesting that I can now save an Array of Character objects since I have now added support for that type with ES2.Save?
jinxology
Posts: 35
Joined: Sat Sep 13, 2014 5:21 pm

Re: ES2Writer/Reader with character list

Post by jinxology »

Ok, no compile errors on supporting the custom "Character" type, but new problem. Here's my code:

Code: Select all

var c:Character = new Character();
ES2.Save.<Character>(c, "char1");  //Errs out on this line
Produces this error:
NullReferenceException: Object reference not set to an instance of an object
ES2Writer.WriteHeader (System.String tag, Key collectionType, .ES2Type valueType, .ES2Type keyType)
ES2Writer.Write[Character] (.Character param, System.String tag)
ES2.Save[Character] (.Character param, System.String identifier)
GameController.SaveGame () (at Assets/Scripts/GameController.js:352)
GameController.Update () (at Assets/Scripts/GameController.js:280)
jinxology
Posts: 35
Joined: Sat Sep 13, 2014 5:21 pm

Re: ES2Writer/Reader with character list

Post by jinxology »

Ok, lol, you can close this topic. The solution was to delete it all out of the Manage Types box, and remove the generated files from the project, then re-add the type. The files must have become a little mangled after all the adding/updating I was doing.
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES2Writer/Reader with character list

Post by Joel »

Glad you've got it sorted!

And just to clarify for anyone else who may come across this thread, if you add support for a type, you'll also be able to save a Collection (array/List/Dictionary/etc) containing that type.

All the best,
Joel
jinxology
Posts: 35
Joined: Sat Sep 13, 2014 5:21 pm

Re: ES2Writer/Reader with character list

Post by jinxology »

Here's some of my code snippets, maybe it will help someone with similar questions. Some points:
  • If you are going to add support for a custom type, you need to add the Class definition to the Plugins folder so that it is compiled early enough for EasySave to recognize it
  • I solved some of my compiler errors by deleting out support for the type and then re-adding it.

Code: Select all

private var allCharacters:List.<Character>;

//Then I load that array with Character objects

ES2.Save.<Character>(allCharacters, "characterList");

//When loading it, I do the following:
if (ES2.Exists("characterList"))
	allCharacters = ES2.LoadList.<Character>("characterList");
I'm pretty impressed with how easy it is once the type is properly supported.
mrm83
Posts: 19
Joined: Tue Jan 26, 2016 6:20 pm

Re: ES2Writer/Reader with character list

Post by mrm83 »

I am having trouble with this..

I created my custom type and it saves and loads fine.
but when i save the custom type in and array, it bombs. List works.. but not an array..

i thought arrays are automatically supported after the type is added.. am i doing something wrong?
public override void Write(object obj, ES2Writer writer)
{
      InventorySlot data = (InventorySlot)obj;
      writer.Write (data.item);
      writer.Write (data.locked);
}
   
public override object Read(ES2Reader reader)
{
      int item = reader.Read<int> ();
      bool locked = reader.Read<bool> ();
      InventorySlot data = new InventorySlot (item, locked);
      return data;
}

InventorySlot test = new InventorySlot (22, false);
inventory [0] = test;
inventory [3] = test;
ES2.Save <InventorySlot[]> (inventory, "test");
//ES2.Save<InventorySlot> (test, "test");
//Debug.Log(ES2.LoadArray <InventorySlot> ("test") [0].item);

error:
Easy Save does not support saving of type MyGame.InventorySlot[].
UnityEngine.Debug:LogError(Object)
ES2Writer:Write(InventorySlot[], ES2Type)
ES2Writer:Write(InventorySlot[], String)
ES2:Save(InventorySlot[], String)
MyGame.Inventory:Start() (at Assets/Scripts/Inventory.cs:19)
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES2Writer/Reader with character list

Post by Joel »

Hi there,

If you're going to define the generic parameter, it should represent the type inside the array rather than be an array.

i.e. This line ...
ES2.Save <InventorySlot[]> (inventory, "test");
... Should be ...
ES2.Save <InventorySlot> (inventory, "test");
However, you don't need to specify the generic parameter on Save methods, only on Load methods. i.e. you could also just do this.
ES2.Save(inventory, "test");
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked